⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basevalidator.cs

📁 asp.net经典案例资料
💻 CS
字号:
namespace snowy
{
  using System;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;

  public abstract class BaseValidator : Label, IValidator
  {
    private bool isValid;
    public bool IsValid {
      get{ return isValid;}
      set{ isValid = value;}
    }

    public string ErrorMessage {
      get {
        object o = ViewState["ErrorMessage"];
        return((o == null) ? String.Empty : (string)o);
      }
      set { ViewState["ErrorMessage"] = value; }
    }

    public string ControlToValidate {
      get { 
        object o = ViewState["ControlToValidate"];
        return((o == null) ? String.Empty : (string)o);
      }
      set { ViewState["ControlToValidate"] = value; }
    }

    public bool EnableClientScript {
      get {
        object o = ViewState["EnableClientScript"];
        return((o == null) ? true : (bool)o);
      }
      set { ViewState["EnableClientScript"] = value; }
    }

    protected override void AddAttributesToRender(HtmlTextWriter writer) 
    {
      base.AddAttributesToRender(writer);
      if (ID == null)
        writer.AddAttribute("id", ClientID);

      if (ControlToValidate.Length > 0)
      {
        Control c = FindControl(ControlToValidate);            
        writer.AddAttribute("controltovalidate", c.ClientID);
      }

      if (ErrorMessage.Length > 0)
        writer.AddAttribute("errormessage", ErrorMessage, true);

      if (!IsValid)
        writer.AddAttribute("isvalid", "False");

      //其他从这个类继承的验证控件必须实现evaluationfunction属性。客户端脚本库将调
      //用动态调用这个属性对应的脚本函数。
      //writer.AddAttribute("evaluationfunction", "RequiredFieldValidatorEvaluateIsValid");
    }

    protected override void OnInit(EventArgs e) {
      base.OnInit(e);
      Page.Validators.Add(this);
    }        

    protected override void OnUnload(EventArgs e) {
      if (Page != null)
        Page.Validators.Remove(this);
      base.OnUnload(e);
    } 

	 //基类必须实现这个方法
    public abstract void Validate(); 

    protected virtual void RegisterValidatorDeclaration() 
    {
      string element = "document.getElementById(\"" + ClientID + "\")";
      Page.RegisterArrayDeclaration("Page_Validators", element);
    }

	  protected virtual void RegisterClientScript()
    {
	    const string scriptFile = "/aspnet_client/system_web/1_0_3705_0/WebUIValidation.js";
      const string script = "<script language=\"javascript\" src=\"" + scriptFile + "\"></script>";

      if (!Page.IsClientScriptBlockRegistered("scriptKey")) 
        Page.RegisterClientScriptBlock("scriptKey", script);

      Page.RegisterOnSubmitStatement("submitKey", "ValidatorOnSubmit();");

      const string ValidatorStartupScript = @"
      <script language=""javascript"">
      var Page_ValidationActive = false;
      ValidatorOnLoad();

      function ValidatorOnSubmit() {
          return ValidatorCommonOnSubmit();
      }
      </script>";

      if(!Page.IsStartupScriptRegistered("startkey"))
        Page.RegisterStartupScript("startkey", ValidatorStartupScript);
    }

    protected override void OnPreRender(EventArgs e) {
      base.OnPreRender(e);
      RegisterValidatorDeclaration();
	    RegisterClientScript();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -