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 Co开发者_高级运维ntrolToCompare too.
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>
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.
加载中,请稍侯......
精彩评论