RequiredFieldValidator for Hidden Input?

G

Guest

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.
 
N

Nathan Sokalski

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!
 
G

Guest

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.
 
N

Nathan Sokalski

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.
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top