CSDN博客

img cnlike

ASP.NET Validator Control 备忘

发表于2004/10/15 21:17:00  584人阅读

Page的OnPreRender被调用前必须要用至少一个Validator(如CustomValidator),否则生成的客户端脚本会缺少:
<script language="javascript" type="text/javascript" src="/aspnet_client/system_web/1_1_4322/WebUIValidation.js"></script>
System.Web.UI.WebControls.BaseValidator


protected override void OnPreRender(EventArgs e)
{
      base.OnPreRender(e);
      this.preRenderCalled = true;
      this.propertiesChecked = false;
      this.renderUplevel = this.DetermineRenderUplevel();
      if (this.renderUplevel)
      {
            this.RegisterValidatorCommonScript();
      }
}
protected void RegisterValidatorCommonScript()
{
      if (!this.Page.IsClientScriptBlockRegistered("ValidatorIncludeScript"))
      {
            string text1 = Util.GetScriptLocation(this.Context);
            string text2 = HttpRuntime.FormatResourceString("Validator_missing_script", text1 + "WebUIValidation.js");
            string text3 = HttpRuntime.FormatResourceString("Validator_wrong_script", "WebUIValidation.js", "125", "/" + Page_ValidationVer + /"");
            string text4 = string.Empty;
            if (this.Page.Request.IsLocal)         //会弹出缺少WebUIValidation.js的错误
            {
                  object[] objArray1 = new object[3] { text2, "125", text3 } ;
                  text4 = string.Format("/r/n<script language=/"javascript/" type=/"text/javascript/">/r/n<!--/r/nvar Page_ValidationActive = false;/r/nif (typeof(clientInformation) != /"undefined/" && clientInformation.appName.indexOf(/"Explorer/") != -1) {{/r/n    if (typeof(Page_ValidationVer) == /"undefined/")/r/n        alert(/"{0}/");/r/n    else if (Page_ValidationVer != /"{1}/")/r/n        alert(/"{2}/");/r/n    else/r/n        ValidatorOnLoad();/r/n}}/r/n/r/nfunction ValidatorOnSubmit() {{/r/n    if (Page_ValidationActive) {{/r/n        return ValidatorCommonOnSubmit();/r/n    }}/r/n    return true;/r/n}}/r/n// -->/r/n</script>/r/n        ", objArray1);
            }
            else
            {
                  text4 = string.Format("/r/n<script language=/"javascript/" type=/"text/javascript/">/r/n<!--/r/nvar Page_ValidationActive = false;/r/nif (typeof(clientInformation) != /"undefined/" && clientInformation.appName.indexOf(/"Explorer/") != -1) {{/r/n    if ((typeof(Page_ValidationVer) != /"undefined/") && (Page_ValidationVer == /"{0}/"))/r/n        ValidatorOnLoad();/r/n}}/r/n/r/nfunction ValidatorOnSubmit() {{/r/n    if (Page_ValidationActive) {{/r/n        return ValidatorCommonOnSubmit();/r/n    }}/r/n    return true;/r/n}}/r/n// -->/r/n</script>/r/n        ", "125");
            }
            this.Page.RegisterClientScriptFileInternal("ValidatorIncludeScript", "javascript", text1, "WebUIValidation.js");
            this.Page.RegisterStartupScript("ValidatorIncludeScript", text4);
            this.Page.RegisterOnSubmitStatement("ValidatorOnSubmit", "if (!ValidatorOnSubmit()) return false;");
      }
}
阅读全文
0 0

相关文章推荐

img
取 消
img