user3234852
user3234852

Reputation: 21

ASP.NET c# Form validation

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> 

Upvotes: 2

Views: 2170

Answers (2)

gbs
gbs

Reputation: 7276

What you have should work because once you set Enabled=false, the validators are disabled. Then even if you explicitly set your textboxes visible, validators won't fire.

So there is something else in your code that is causing the behavior.

Are you doing something in javascript with your panel/textboxes?

Are you using the same postback control that is firing validators or different?

Sample code:

$(function () {
               //btnTogglePanel is the button that show/hide the panel
                $("#btnTogglePanel").click(function () {
                    $("#pnlOpv").toggle();
                    var pnlisvisible = $("#pnlOpv").is(':visible');
                    var revOpv = document.getElementById("revOpv");
                    var rfvOpv = document.getElementById("rfvOpv");
                    ValidatorEnable(revOpv, pnlisvisible);
                    ValidatorEnable(rfvOpv, pnlisvisible);
                });
            });

Upvotes: 0

Sergio A.
Sergio A.

Reputation: 401

The fact that the panel has in the style "display: none" doesn't mean that it's invisible. For the ASP.Net viewstate the panel and everything on it is visible. It will be invisible only if you set the ASP.Net Property "visible=false".

Now, for those controls that you're hiding/showing with styling and javascript (I guess) I would suggest to check the attributes at runtime and fire the validations based on it, although, I suspect that the attributes won't contain anything referring to the style (again, display: none).

I suspect that's what's causing you problems when firing the validations and checking for those that are visible/not visible.

Let me know if that helps or have any other questions!

Upvotes: 1

Related Questions