Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6116117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:10:10+00:00 2026-05-23T15:10:10+00:00

I am experiencing a problem with an ASPX page not executing a CustomValidator. The

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:10:11+00:00Added an answer on May 23, 2026 at 3:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm experiencing this weird problem which my scrollbar jumps by itself to somewhere that
I'm experiencing a problem on a test-machine which is running in a quite restrictive
I'm experiencing a weird problem which I hope someone in here may be able
I'm experiencing the same problem in this previous stackoverflow.com post . Specifically, I seem
Our team has been experiencing a recurring problem with velocity templates. Upon rendering, some
i'm currently experiencing a little problem. I'm using nhibernate with around 40 entities mapped
Im experiencing problem with webclient to parse some very sensitive webpage. Somehow, webclient failed
I am experiencing problem in my application with OutOfMemoryException. My application can search for
I am experiencing problems with Forms Authentication. When I try to load my Login.aspx
I am experiencing a problem with using a constant defined in a configuration file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.