I have an ASP.NET web form with all validators disabled. I want to enable validators only for those controls that are visible to user AFTER Submit button is clicked.
Here is my function:
protected void Submit_Click(object sender, System.EventArgs e) {
if (ddlSite.Visible){
rfvSite.Enabled = true;
base.Validate();
}
else {
rfvSite.Enabled = false;
}
The above code is working fine for most of the controls. But I have a couple of controls with display set to none. I make them visible only if certain selections are made. These invisible controls are causing problem. E.g. The pnlOpv panel contain a textbox which has two validators revOpv and rfvOpv.
if (pnlOpv.Visible){
revOpv.Enabled = true;
rfvOpv.Enabled = true;
}
else {
revOpv.Enabled = false;
rfvOpv.Enabled = false;
}
The above code is giving me problem because even though this above textbox is not visible because it is contained inside invisible panel. For some strange reason, the form thinks validators are enabled and would return page as invalid. Do you see any errors?
<asp:panel id="pnlOpv" style="margin:0px; padding:0px; display: none;" runat="server">
<label for="txtOpoo" id="opo" style="display:inline; margin-top:5px;"><strong>Other Place of Visit</strong></label>
<asp:TextBox type="text" id="txtOpv" tabindex="2" size="20" maxlength="50" runat="server" style="display:inline; margin-top:5px; background-color:#FCFCFC" EnableViewState="true" />
<asp:RegularExpressionValidator ID="revOpv" runat="server"
ControlToValidate="txtOpv"
ValidationExpression="^[a-zA-Z0-9''-'-,.\s]{1,50}$"
Display="Dynamic"
Font-Names="verdana" Font-Size="10pt" Enabled="false" EnableClientScript="true" EnableViewState="true"> Invalid format.
</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator id="rfvOpv" runat="server"
ControlToValidate="txtOpv"
Display="Dynamic"
Font-Names="Verdana" Font-Size="10pt" Enabled="false" EnableViewState="true" EnableClientScript="true">
Please type other place of visit.
</asp:RequiredFieldValidator>
</asp:panel>