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

  • Home
  • SEARCH
  • 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 1092453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:41:05+00:00 2026-05-16T23:41:05+00:00

I am adding a custom validator to the page programmatically on click of a

  • 0

I am adding a custom validator to the page programmatically on click of a button, then validating the page and checking the IsValid property of the page. but the IsValid property is always returning true. Please help. here is the code. I need to add custom validator dynamically to show validation messages from business object. I am setting the IsValid property of the custom validator to false, so I expect the IsValid property of the Page to return false as well after validation. can’t understand what I am doing wrong here.

    protected void Button1_Click(object sender, EventArgs e)
{
    var validator = new CustomValidator();
    validator.IsValid = false;
    validator.ErrorMessage = "The input is invalid";
    validator.ValidationGroup = "vgCustom";
    Page.Validators.Add(validator);
    ValidationSummary1.ValidationGroup = "vgCustom";
    Page.Validate("vgCustom");
    Label1.Text = Page.IsValid ? "The Page is valid" : "The Page is Invalid";
}

and here is the HTML mark-up

<html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
    <title></title>
 </head>
 <body>
    <form id="form1" runat="server">
    <div>
        <asp:ValidationSummary ID="ValidationSummary1" runat="server"/>
        <asp:Button ID="Button1" runat="server" Text="Validate" OnClick="Button1_Click" />
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
 </body>
</html>
  • 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-16T23:41:06+00:00Added an answer on May 16, 2026 at 11:41 pm

    BaseValidator.Validate Method:

    Use the Validate method to perform
    validation on the associated input
    control. This method allows you to
    programmatically perform validation on
    the input control. The IsValid
    property is automatically updated with
    the validation results.

    So validator.IsValid is being reset to its default value (True) when you call Page.Validate("vgCustom"). Whereas with the ServerValidateEventHandler, your code is setting IsValid on Page.Validate("vgCustom") instead of letting it be reset to the default. If you move validator.IsValid = false to after the call to Page.Validate("vgCustom"), the page should fail to validate as expected.

    I prefer to use the following pattern though:

    /// <summary>
    /// A validator that fails unconditionally. Useful if you need to do
    /// validation entirely in the code-behind, yet still integrate with
    /// the standard ASP.NET validation framework.
    /// </summary>
    public class FailValidator : BaseValidator {
        protected override bool ControlPropertiesValid() {
            // make setting ControlToValidate optional
            return true;
        }
    
        protected override bool EvaluateIsValid() {
            return false;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way of adding custom keyboard shortcuts to a specific page? In
I got below code from http://msdn.microsoft.com/en-us/library/dd584174(office.11).aspx for adding custom property in webpart tool pane.
I am working on adding a custom accessory view, (a button) to a UITableViewCell,
I have a method that is adding custom objects to an array in a
How is working with Magento when it comes to adding custom functionality? From an
I've a question concerning handling touch events for CustomView .I'm adding custom view's dynamically
By adding a custom handler to the SiteMapResolve event, I can update sitemap url's
I'm adding a custom context menu item to documents (and not folders) in a
i am working on one application which requires the feature of adding the custom
I customized the content of Defect in my Rally workspace adding a new custom

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.