By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,275 Members | 947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,275 IT Pros & Developers. It's quick & easy.

RequiredFieldValidator for Hidden Input?

P: n/a
WB
Hi,

I need to validate a hidden input in my webform to ensure that it's got
value. The controls look something like this:
<input id="HidSelectedStates" type="hidden" runat="server" /><br /><br />
<asp:Button ID="BtnSubmit" runat="server" OnClick="BtnSubmit_Click"
Text="Submit" ValidationGroup="submitStates" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="HidSelectedStates" runat="server" ErrorMessage="Please
select the states" ValidationGroup="submitStates" />
However, this gives the following error

System.Web.HttpException: Control 'HidSelectedStates' referenced by the
ControlToValidate property of 'RequiredFieldValidator1' cannot be validated

How can I get around this?

WB.
Mar 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I don't think the Validator Controls are capable of validating hidden input
fields. The reason for this is that

1. <input type="hidden"> does not have a corresponding class in the
System.Web.UI.WebControls namespace
2. Because the value attribute can only be modified using code, the value
should be validated before it is assigned to the tag, therefore there is no
need for a validator other than server-side code if the value needs compared
against something the client does not have access to, such as a database

What is your reason for needing a validator for the hidden field? It sounds
like you are just keeping track of what states the user has selected. I
would select doing this using a multiple-selection ListBox like the
following:

<asp:ListBox id="HidSelectedStates" runat="server"
SelectionMode="Multiple"></asp:ListBox>

If you want more suggestions, let me know in more detail what you are doing
with the hidden input. Good Luck!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"WB" <WB@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
Hi,

I need to validate a hidden input in my webform to ensure that it's got
value. The controls look something like this:
<input id="HidSelectedStates" type="hidden" runat="server" /><br /><br />
<asp:Button ID="BtnSubmit" runat="server" OnClick="BtnSubmit_Click"
Text="Submit" ValidationGroup="submitStates" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="HidSelectedStates" runat="server" ErrorMessage="Please
select the states" ValidationGroup="submitStates" />
However, this gives the following error

System.Web.HttpException: Control 'HidSelectedStates' referenced by the
ControlToValidate property of 'RequiredFieldValidator1' cannot be
validated

How can I get around this?

WB.

Mar 14 '06 #2

P: n/a
WB
Hi Nathan,

Thanks for the reply.

Actually the reason I'm using a hidden control is that I have a treeview
with checkboxes of states (eg. California, Oregon etc) for users to select.
This treeview is built by a 3rd party software and the checkboxes are not
server control. So I have a client script to copy the selected boxes value to
the hidden field so that I can use the value on PostBack.

A ListBox won't do 'cuz I need to show the hierachy of countries > states.

Any suggestions?
WB.
"Nathan Sokalski" wrote:
I don't think the Validator Controls are capable of validating hidden input
fields. The reason for this is that

1. <input type="hidden"> does not have a corresponding class in the
System.Web.UI.WebControls namespace
2. Because the value attribute can only be modified using code, the value
should be validated before it is assigned to the tag, therefore there is no
need for a validator other than server-side code if the value needs compared
against something the client does not have access to, such as a database

What is your reason for needing a validator for the hidden field? It sounds
like you are just keeping track of what states the user has selected. I
would select doing this using a multiple-selection ListBox like the
following:

<asp:ListBox id="HidSelectedStates" runat="server"
SelectionMode="Multiple"></asp:ListBox>

If you want more suggestions, let me know in more detail what you are doing
with the hidden input. Good Luck!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"WB" <WB@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
Hi,

I need to validate a hidden input in my webform to ensure that it's got
value. The controls look something like this:
<input id="HidSelectedStates" type="hidden" runat="server" /><br /><br />
<asp:Button ID="BtnSubmit" runat="server" OnClick="BtnSubmit_Click"
Text="Submit" ValidationGroup="submitStates" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="HidSelectedStates" runat="server" ErrorMessage="Please
select the states" ValidationGroup="submitStates" />
However, this gives the following error

System.Web.HttpException: Control 'HidSelectedStates' referenced by the
ControlToValidate property of 'RequiredFieldValidator1' cannot be
validated

How can I get around this?

WB.


Mar 14 '06 #3

P: n/a
Well, if the control is from a 3rd party, I think you're stuck with either
writing client-side JavaScript to validate it or validating it server-side.
An idea for validating it client-side would be to save in a global variable
what you were trying to save as the hidden field's value. Then add an
onSubmit event that validates and, if it is valid, appends this variable's
value to the URL as a querystring. This will allow you to submit it as if it
were part of the form. It might require a little extra code, but it's the
best I can come up with for a control that you can only access with
JavaScript.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"WB" <WB@discussions.microsoft.com> wrote in message
news:F6**********************************@microsof t.com...
Hi Nathan,

Thanks for the reply.

Actually the reason I'm using a hidden control is that I have a treeview
with checkboxes of states (eg. California, Oregon etc) for users to
select.
This treeview is built by a 3rd party software and the checkboxes are not
server control. So I have a client script to copy the selected boxes value
to
the hidden field so that I can use the value on PostBack.

A ListBox won't do 'cuz I need to show the hierachy of countries > states.

Any suggestions?
WB.
"Nathan Sokalski" wrote:
I don't think the Validator Controls are capable of validating hidden
input
fields. The reason for this is that

1. <input type="hidden"> does not have a corresponding class in the
System.Web.UI.WebControls namespace
2. Because the value attribute can only be modified using code, the value
should be validated before it is assigned to the tag, therefore there is
no
need for a validator other than server-side code if the value needs
compared
against something the client does not have access to, such as a database

What is your reason for needing a validator for the hidden field? It
sounds
like you are just keeping track of what states the user has selected. I
would select doing this using a multiple-selection ListBox like the
following:

<asp:ListBox id="HidSelectedStates" runat="server"
SelectionMode="Multiple"></asp:ListBox>

If you want more suggestions, let me know in more detail what you are
doing
with the hidden input. Good Luck!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"WB" <WB@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
> Hi,
>
> I need to validate a hidden input in my webform to ensure that it's got
> value. The controls look something like this:
>
>
> <input id="HidSelectedStates" type="hidden" runat="server" /><br /><br
> />
> <asp:Button ID="BtnSubmit" runat="server" OnClick="BtnSubmit_Click"
> Text="Submit" ValidationGroup="submitStates" />
> <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
> ControlToValidate="HidSelectedStates" runat="server"
> ErrorMessage="Please
> select the states" ValidationGroup="submitStates" />
>
>
> However, this gives the following error
>
> System.Web.HttpException: Control 'HidSelectedStates' referenced by the
> ControlToValidate property of 'RequiredFieldValidator1' cannot be
> validated
>
> How can I get around this?
>
> WB.


Mar 14 '06 #4

P: n/a
WB
Hi,

Thanks for the advice.

I've decided to just user a label control like this:
<asp:Label ID="NoStateSelected" runat="server" CssClass="error" Text="Please
select states" Visible="False" />

And if no state is selected (ie. empty hidden field), I will turn this
lablel's visible attribute to true and won't process the form...
WB.
"Nathan Sokalski" wrote:
Well, if the control is from a 3rd party, I think you're stuck with either
writing client-side JavaScript to validate it or validating it server-side.
An idea for validating it client-side would be to save in a global variable
what you were trying to save as the hidden field's value. Then add an
onSubmit event that validates and, if it is valid, appends this variable's
value to the URL as a querystring. This will allow you to submit it as if it
were part of the form. It might require a little extra code, but it's the
best I can come up with for a control that you can only access with
JavaScript.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"WB" <WB@discussions.microsoft.com> wrote in message
news:F6**********************************@microsof t.com...
Hi Nathan,

Thanks for the reply.

Actually the reason I'm using a hidden control is that I have a treeview
with checkboxes of states (eg. California, Oregon etc) for users to
select.
This treeview is built by a 3rd party software and the checkboxes are not
server control. So I have a client script to copy the selected boxes value
to
the hidden field so that I can use the value on PostBack.

A ListBox won't do 'cuz I need to show the hierachy of countries > states.

Any suggestions?
WB.
"Nathan Sokalski" wrote:
I don't think the Validator Controls are capable of validating hidden
input
fields. The reason for this is that

1. <input type="hidden"> does not have a corresponding class in the
System.Web.UI.WebControls namespace
2. Because the value attribute can only be modified using code, the value
should be validated before it is assigned to the tag, therefore there is
no
need for a validator other than server-side code if the value needs
compared
against something the client does not have access to, such as a database

What is your reason for needing a validator for the hidden field? It
sounds
like you are just keeping track of what states the user has selected. I
would select doing this using a multiple-selection ListBox like the
following:

<asp:ListBox id="HidSelectedStates" runat="server"
SelectionMode="Multiple"></asp:ListBox>

If you want more suggestions, let me know in more detail what you are
doing
with the hidden input. Good Luck!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"WB" <WB@discussions.microsoft.com> wrote in message
news:A4**********************************@microsof t.com...
> Hi,
>
> I need to validate a hidden input in my webform to ensure that it's got
> value. The controls look something like this:
>
>
> <input id="HidSelectedStates" type="hidden" runat="server" /><br /><br
> />
> <asp:Button ID="BtnSubmit" runat="server" OnClick="BtnSubmit_Click"
> Text="Submit" ValidationGroup="submitStates" />
> <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
> ControlToValidate="HidSelectedStates" runat="server"
> ErrorMessage="Please
> select the states" ValidationGroup="submitStates" />
>
>
> However, this gives the following error
>
> System.Web.HttpException: Control 'HidSelectedStates' referenced by the
> ControlToValidate property of 'RequiredFieldValidator1' cannot be
> validated
>
> How can I get around this?
>
> WB.


Mar 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.