如何扩展compareValidator,以便我可以检查,如果用户在ControlToValidate中写了一些文本,那么他也必须在ControlToCompare中写一些文本。

最佳答案

尝试:

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>

10-08 19:44