coure2011
coure2011

Reputation: 42464

Extending Asp.net CompareValidator

How to extend a compareValidator so i can check, if user has written some text in ControlToValidate then he must write some text in ControlToCompare too.

Upvotes: 3

Views: 540

Answers (2)

Raghu A
Raghu A

Reputation: 441

You don't need to extend CompareValidator to solve this. Use RequiredFieldValidator on both controls to validate they are not empty. This approach have the advantage of validating on the client side as well so avoiding a round trip to server.

Upvotes: 1

m3kh
m3kh

Reputation: 7941

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>

Upvotes: 1

Related Questions