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
2010-06-12 05:50:13
How i will use this validator? <asp:ExtendedCompareValidator>?
coure06
2010-06-12 06:42:08
You don't need to change anything. Use traditional `CompareValidator` control and ASP.NET maps it with new one.
Mehdi Golchin
2010-06-12 07:24:53
No its not working. It has the same behavior as of normal CompareValidator control.
coure06
2010-06-12 07:42:06
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
2010-06-12 08:16:41
<add tagType="System.Web.UI.WebControls.CompareValidator, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" mappedTagType="CustomValidators.ExtendCompareValidator, App_Code"/>
coure06
2010-06-12 11:38:50
`<add tagType="System.Web.UI.WebControls.CompareValidator, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" mappedTagType="MyWebApp.ExtendedCompareValidator"/>`
Mehdi Golchin
2010-06-12 12:53:47