Search code examples
asp.netpanelcustomvalidator

CustomValidator not executing on second step of a multi-panel Page


I am experiencing a problem with an ASPX page not executing a CustomValidator. The Page consists of 3 ASP Panels which swap visibility for each step in a 3 step process. The first step/panel functions as expected, executing all CustomValidators when I click the submit button. If valid, the button click hides its panel and shows the second panel for step #2 which contains another CustomValidator. When clicking the submit button on this second panel, the CustomValidator never executes and the Page always reports that it IsValid.

I have reproduced this behavior in a small, example app. Here is the relevant code...

Default.aspx

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
    <asp:Panel ID="Panel1" runat="server" Visible="true">
        <div>
            <asp:CustomValidator 
                ID="CustomValidator1" 
                runat="server" 
                ControlToValidate="TextBox1"
                ValidateEmptyText="true"
                Display="Dynamic" 
                OnServerValidate="CustomValidator1_ServerValidate">
            </asp:CustomValidator>
        </div>
        <div>
            <asp:Label ID="Label1" runat="server" Text="Type in anything:" AssociatedControlID="TextBox1" />
            &nbsp;
            <asp:TextBox ID="TextBox1" runat="server" />
        </div>
        <div>
            <asp:Button ID="Button1" runat="server" Text="Show Panel #2" OnClick="Button1_Click" />
        </div>
    </asp:Panel>
    <asp:Panel ID="Panel2" runat="server" Visible="false">
        <div>
            <asp:CustomValidator 
                ID="CustomValidator2" 
                runat="server" 
                Display="Dynamic" 
                OnServerValidate="CustomValidator2_ServerValidate">
            </asp:CustomValidator>
        </div>
        <div>
            <asp:Button ID="Button2" runat="server" Text="I should cause an Exception..." OnClick="Button2_Click" />
        </div>
    </asp:Panel>
    <asp:Panel ID="Panel3" runat="server" Visible="false">
        <p>An exception should have been thrown. :(</p>
    </asp:Panel>
</asp:Content>

Default.aspx.cs

public partial class Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
        Panel3.Visible = false;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            Panel3.Visible = false;
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = true;
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string userEnteredText = TextBox1.Text;
        if (string.IsNullOrEmpty(userEnteredText))
        {
            CustomValidator1.Text = "Text is required!";
            args.IsValid = false;
        }
        else if (!userEnteredText.ToLower().Equals("anything"))
        {
            CustomValidator1.Text = "You didn't type 'anything'! ;)";
            TextBox1.Text = null;
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        throw new Exception("This ServerValidate() method never triggers!");
    }
}

I don't understand why the CustomValidator2 method is never executing. Can anyone explain this behaviour?


Solution

  • As you're not setting the ControlToValidate property in your scenario set the property ValidateWhenEmpty to true on the CustomValidator.

    The CustomValidator will not be evaluated when the ControlToValidate is blank unless ValidateWhenEmpty is true.

    UPDATE:

    Ok this was wrong. But do you really need to set visibility of the panels in the Page_Load? You've done it already declaratively in the .aspx. If you remove it from Page_Load, the validator works. I suppose it does not work if the validator is Visible=false or is inside a containing control that is Visible=false.