Asp.net Extension CompareValidator

How to extend compareValidator so that I can check if the user wrote some text in ControlToValidate, then he should also write some text in ControlToCompare.

+3
source share
2 answers

Try:

public class ExtendedCompareValidator : CompareValidator
{

    protected override void OnPreRender(EventArgs e)
    {
        if (!string.IsNullOrEmpty(this.ControlToValidate) && string.IsNullOrEmpty(this.ControlToCompare))
            throw new HttpException("You have to set the 'ControlToCompare' property.");

        base.OnPreRender(e);
    }

}

Web.config

<pages>
  <tagMapping>
    <add tagType="System.Web.UI.WebControls.CompareValidator, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" mappedTagType="MyWebApp.ExtendedCompareValidator, MyWebApp"/>
  </tagMapping>
</pages>
+1
source

You do not need to expand CompareValidatorto solve this problem. Use RequiredFieldValidatorfor both controls to verify that they are not empty. The advantage of this approach is client-side validation, so avoid traveling together to the server.

+1
source

Source: https://habr.com/ru/post/1749706/


All Articles