views:

67

answers:

1

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.

+1  A: 

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>
Mehdi Golchin
How i will use this validator? <asp:ExtendedCompareValidator>?
coure06
You don't need to change anything. Use traditional `CompareValidator` control and ASP.NET maps it with new one.
Mehdi Golchin
No its not working. It has the same behavior as of normal CompareValidator control.
coure06
Did you add the above code snippet to the `pages` section of web.config? Also don't forgot to change the `mappedTagType` property.
Mehdi Golchin
<add tagType="System.Web.UI.WebControls.CompareValidator, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" mappedTagType="CustomValidators.ExtendCompareValidator, App_Code"/>
coure06
`<add tagType="System.Web.UI.WebControls.CompareValidator, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" mappedTagType="MyWebApp.ExtendedCompareValidator"/>`
Mehdi Golchin