2

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> 
2
  • Why dont you show the model? to see what is required and check the validation you have there and the one you want to achieve in the code. Commented Feb 11, 2014 at 19:02
  • Why don't you try using Validation Group? Commented Feb 11, 2014 at 19:02

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

I'll add visible=false and see if it works instead of display:none. Yes I'm using JavaScript to hide/unhide panels. How do I set the asp.net visible=true using JavaScript?
I don't think you can do that using Javascript. You need to do that in your managed code, from the server side.
0

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);
                });
            });

5 Comments

I know for sure I'm able to turn validator on because some of the controls in the form are always visible but their validators are disabled by default. I enable them after Submit button is clicked. It's only those control which are hidden are causing this problem. This form is live and very long so, I need to find a way to fix this problem. There is no way I can start from scratch.
I see you said in other comment that you are showing/hiding panel on the client-side. So in that case you need to enable/disable the validator controls on the client-side. e.g aspsnippets.com/Articles/… Let me know if you need help with code specific to your panel.
Will do. Thx GBS. I'll be in touch because this is going over my understanding of asp.net and I'm going to need some help with code. Yes I'm enabling/disabling validators in code-behind page when user clicks Submit button.
So far I'm very frustrated with this forum software. I'm leaving as there is not much and posting anything is problematic.
Do you mean you are having problem with stackoverflow? If yes, please explain what issue are you facing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.