Validating a hidden control?

B

Bill Cohagan

Is it possible to have a validation control whose ControlToValidate is in
fact a hidden control? When I try this I find that the validator apparently
does not fire. If I make the control Visible="true", then the validator
fires as expected.

I realize that in a sense it doesn't make sense to validate such a control
since the user can't do anything (directly) to cure the problem. In my case
however the hidden control contains information representing a bit of state.
A simplified case would be that I don't want button A to submit until button
B has already been clicked (and the form submitted.) I "remember" whether
the form has had a "B submit" in a hidden field. I use validation groups to
differentiate between the two submit buttons.

Any suggestions?

Bill
 
G

Guest

Hi Bill,

When you set a control's visible to false on server-side, the control
actually is not rendered to client-side. Hence the validator has nothing to
validate on client-side.

You should 'hidden' the control on client-side.

Suppose you want to hidden a textbox (ID = txtHidden), using following code
in Page_Load:

string hiddenTxt = "<script>var txt = document.getElementById('txtHidden');
txt.style.visibility='hidden';</script>";
Page.RegisterStartupScript("hidden", hiddenTxt);

HTH

Elton Wang
 
B

Bill Cohagan

Elton
On closer examination I find that you are incorrect. In fact the control
*is* rendered; albeit with a munged ID/Name such that the validator can't
see it under the originally specified ID. Use "View Source" to verify
this....

Bill
 
S

Steven Cheng[MSFT]

Hi Bill,

As for the ASP.NET web server controls(include html server controls), if we
set their Visible property to "false", they will not be rendered out so
that Validators on the page can not correctly reference to them. To make
the validator controls be able to validate them and also make them hidden,
I think you can consider use the clientside "display" style of the html
element to hide the control. e.g:

<input type="text" runat="server" id="txtClient" value="2"
style="display:none"/><br />

In such case, the control is hidden on the page, but the validator is still
able to validate it.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Glad that this is of assistance.

If there's anything else we can help, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top