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 974121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:23:02+00:00 2026-05-16T03:23:02+00:00

I want to extend the asp.net validators such that I can make one validator

  • 0

I want to extend the asp.net validators such that I can make one validator dependent on another. The situation I have is that we have to validate a date in a textbox. Normally I would just use a combination of a RequiredFieldValidator (to ensure the date is provided), CompareValidator (to ensure the date is a date) and finally a RangeValidator (to ensure the date is within the required limit).

The problem with this is that the validators do not depend on each other, so as a result the user would see possibly all three messages at once for each validator when really all we want them to see is the most relevant message, i.e. if they entered “abc” in the date text box it would not be appropriate to show them the message saying the date was not in the valid range (even though technically I suppose this is true).

Currently to provide this kind of functionality we use a CustomValidator and just put all three validations within the server validate event handler and change the error message programmatically depending on what validation failed.

I would like to standardize this a bit more as it happens quite a bit in this application, I figure if I can make the validators dependent on each other this will solve the problem and also allow us to make use of the client side validation rather than having to do a postback especially to handle the custom validation.

The idea is that if one validator is dependent on another if that “master” is valid then the depended will perform its normal validation (EvaluateIsValid()) otherwise if the master validator is not valid then the other dependent validators will be valid.

I have come up with the following solution by inheriting from the various validator controls that already have been provided in the framework.

public class RequiredFieldDependentValidator : RequiredFieldValidator
{
    [Description("The validation control to depend on for determining if validation should occur")]
    public string ValidationControlToDependOn
    {
        get
        {
            object obj = ViewState["ValidationControlToDependOn"];
            if (obj != null) return (string) obj;
            return null;
        }
        set
        {
            Control control = FindControl(value);
            if (control is IValidator)
                ViewState["ValidationControlToDependOn"] = value;
            else
                throw new HttpException("ValidationControlToDependOn is not a validation control");
        }
    }

    protected override bool EvaluateIsValid()
    {
        IValidator validationControlToDependOn = FindControl(ValidationControlToDependOn) as IValidator;

        if(validationControlToDependOn != null)
        {
            return !validationControlToDependOn.IsValid || base.EvaluateIsValid();
        }

        return base.EvaluateIsValid();
    }

Currently I have just coded it for the RequiredFieldValidator, ideally I would like to provide this functionality for all of the validators but I cannot see a way to do this without copying the above code into a similar class for each individual type of validator I want to provide this functionality for thus if there are any problems I’m going to have to go back and change this code on each validator type individually.

Is there a way I can “centralise” this code and have it easily used in the validators without having to write the entire validators from scratch just so I can change the class they inherit from further down the line.

Cheers,

  • 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-16T03:23:03+00:00Added an answer on May 16, 2026 at 3:23 am

    You might want to look into a WebControlAdapter.

    Basically allows you to override certain methods of webcontrols (conditionally for some browsers if need, but here can be for all).

    In your case, you would want to override the EvaluateIsValid method and check if the control has any dependency on a ‘parent’ validator.

    As an example, a TextBox adapter we recently created to render a ‘maxlength’ attribute to the control.

    Public Class TextBoxAdapter
            Inherits WebControlAdapter
    
            Private ReadOnly Property TextBoxControl() As TextBox
                Get
                    Return DirectCast(MyBase.Control, TextBox)
                End Get
            End Property
    
            Protected Overrides Sub RenderBeginTag(ByVal writer As System.Web.UI.HtmlTextWriter)
                If TextBoxControl.TextMode = TextBoxMode.MultiLine AndAlso TextBoxControl.MaxLength > 0 Then
                    writer.AddAttribute("maxlength", TextBoxControl.MaxLength.ToString)
                End If
    
                MyBase.RenderBeginTag(writer)
            End Sub
        End Class
    

    To use it, just create a .browser file in your App_Browsers directory and setup the adapter there:

    <browsers>
        <browser refID="Default">
            <controlAdapters>
                <adapter controlType="System.Web.UI.WebControls.TextBox"
                   adapterType="TextBoxAdapter" />
            </controlAdapters>
        </browser>
    </browsers>
    

    The only complication that still remains in your case is how to store the dependent validator in order for the EvaluateIsValid to have access to this directory. You might consider a non-rendered control like Onof suggested or else Viewstate/Cookie/other storage mechanism.

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

Sidebar

Related Questions

I want to extend the standard asp.net email regex validator to exclude a specific
I want to extend the IEnumerable class but only for types that can be
I have a class PlaylistTrack that I want to extend class Song. When I
I have a Django template that I want to extend in multiple places. In
I have a SVG file that I want to extend by adding onclick handlers
(sorry for the english) I have a ASP .net webservice that get data from
i want to use asp.net membership provider for my own database i have own
On my C# asp.net webform I have a search page that has roughly 20
Distributing ASP.NET user controls across different projects can be really useful when you want
I like ASP.Net MVC Authorize attribute, I can extend it and build my own

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.