zoukankan      html  css  js  c++  java
  • asp.net注册脚本块

        这段时间在研究asp.net中自动注册脚本块技术,以避免重写烦杂的脚本内容,感谢csdn的saucer讲解。自定义WebForm类,继承Page,从而可以在aspx页面上自动生成脚本包含文件和脚本初始化事件。

        先介绍一下System.Web.UI.Page类中的RegisterClientScriptBlock 和RegisterStartupScript 方法:

    1。RegisterClientScriptBlock:


    public virtual void RegisterClientScriptBlock(
       string key,
       string script
    );

    参数

    key
    标识脚本块的唯一键。
    script
    发送到客户端的脚本的内容。

    备注

    客户端脚本刚好在 Page 对象的 <form runat= server> 元素的开始标记后发出。脚本块是在呈现输出的对象被定义时发出的,因此必须同时包括 <script> 元素的两个标记。

    通过使用关键字标识脚本,多个服务器控件实例可以请求该脚本块,而不用将其发送到输出流两次。

    具有相同 key 参数值的任何脚本块均被视为重复的。

    注意   请记住在脚本周围加入 HTML 注释标记,以便在请求的浏览器不支持脚本时脚本不会呈现。
    2。RegisterStartupScript:

    public virtual
    void RegisterStartupScript(
       string key,
       string script
    );

    参数

    key
    标识脚本块的唯一键。
    script
    要发送到客户端的脚本的内容。

    备注

    RegisterClientScriptBlock 方法类似,该方法在 Page 对象的 <form runat= server> 元素的结束标记之前发出该脚本。发出脚本块时已定义了呈现该页的对象时,因此必须同时包括 <script> 元素的两个标记。

    通过使用 key 标识脚本,多个服务器控件实例可以请求该脚本块,而不用将其发送到输出流两次。

    具有相同 key 参数值的任何脚本块均被视为重复的。

    注意   请记住在脚本周围加入 HTML 注释标记,以便在请求的浏览器不支持脚本时脚本不会呈现。


    下面为自定义类WebForm.cs文件:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    using System.Xml.XPath;

    namespace Prolink
    {
     namespace Web
     {
      namespace UI
      {

       /// <summary>
       /// WebForm 的摘要说明。
       /// </summary>
       public class WebForm : Page {
        XmlDocument doc = new XmlDocument();

        public WebForm(){ 
      
        }

        protected override void OnLoad(System.EventArgs e)
        {
         base.OnLoad(e);
                        OutputScripts();
        }

        /// <summary>
        /// 输出所有脚本块
        /// </summary>
        private void OutputScripts() {
         if (!this.IsClientScriptBlockRegistered("body")) {
          this.RegisterClientScriptBlock("body","<script language=\"javascript\" src=\"" + System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "js\\Body.js" + "\"></script>");
         }
         if (!this.IsStartupScriptRegistered("body")) {
          this.RegisterStartupScript("body","<script language=\"javascript\"><!--installBodyDefaultListener(document.body);//--></script>");
         }
         doc.Load(GetXmlPhysicalApplicationPath());
         GetScript(this);
        }

        /// <summary>
        /// 注册页面所有脚本块
        /// </summary>
        /// <param name="c">某个控件类</param>
        private void GetScript(Control c) {
         if (c.ID != null) {
          GetComponent(c);
         }
         foreach(Control cc in c.Controls) {
          GetScript(cc);
         }
        }

        private void GetComponent(Control c) {
         string strClass = c.GetType().ToString();
         XmlNodeList xnl = doc.GetElementsByTagName("component");
         if (xnl.Count > 0) {
          for (int i = 0; i < xnl.Count; i++) {
           XmlNode xn = xnl[i];
           XmlAttributeCollection xac = xn.Attributes;
           if (xac.Count > 0){
            for (int j = 0; j < xac.Count; j++) {
             XmlAttribute xa = xac[j];
             if ((xa.Name).Equals("class") && ((xa.Value).Equals(strClass))) {
              GetFileReference(c,xac.GetNamedItem("init-method").Value,xac.GetNamedItem("reference-file").Value);
             }
             else {
              continue;
             }
            }
           }
          }
         }
        }

        private void GetFileReference(Control c, string initMethod, string fileName) {
         XmlNodeList xnl = doc.GetElementsByTagName("file-reference");
         ArrayList fileNameList = new ArrayList();
         fileNameList.Add(fileName);

         if (xnl.Count > 0) {
          for (int i = 0; i < xnl.Count; i++) {
           XmlNode xn = xnl[i];
           XmlAttributeCollection xac = xn.Attributes;
           if (xac.Count > 0) {
            for (int j = 0; j < xac.Count; j++) {
             XmlAttribute xa = xac[j];
             if ((xa.Name.Equals("name")) && (xa.Value.Equals(fileName))) {
              if (xn.HasChildNodes) {
               XmlNodeList xnlChild = xn.ChildNodes;
               for (int k = 0; k < xnlChild.Count; k++) {
                fileNameList.Add(xnlChild.Item(k).Attributes.GetNamedItem("name").Value);
               }
              }
             }
            }
           }
          }
         }
         OutputScript(c,initMethod,fileNameList);
        }

        private void OutputScript(Control c, string initMethod, ArrayList fileNameList) {
         string fileName;
         string filePath;
         string sScript;
         if (fileNameList.Count > 0) {
          for (int i = 0; i < fileNameList.Count; i++) {
           fileName = fileNameList[i].ToString();
           filePath = GetFilePath(fileName);
           if (filePath.Equals("")) return;
         
           if (!this.IsClientScriptBlockRegistered(fileName)) {
            sScript = "<script language=\"javascript\" src=\"" + System.Web.HttpContext.Current.Request.PhysicalApplicationPath + filePath + "\"></script>";
            this.RegisterClientScriptBlock(fileName,sScript);
           }
          }
          if (!this.IsStartupScriptRegistered(c.ID)) {
           sScript = "<script language=\"javascript\"><!--" + initMethod + "(document.all." + c.ID + ");//--></script>";
           this.RegisterStartupScript(c.ID,sScript);
          }
         }
        }

        private string GetFilePath(string fileName) {
         string strFileName = "";
         XmlNodeList xnl = doc.GetElementsByTagName("file-name");
         if (xnl.Count > 0) {
          for (int i = 0; i < xnl.Count; i++) {
           XmlNode xn = xnl[i];
           XmlAttributeCollection xac = xn.Attributes;
           if (xac.Count > 0){
            for (int j = 0;j < xac.Count; j++) {
             XmlAttribute xa = xac[j];
             if (xa.Name.Equals("name") && xa.Value.Equals(fileName)) {
              strFileName = xac.GetNamedItem("src").Value;
             }
            }
           }
          }
         }
         return strFileName;
        }

        /// <summary>
        /// 获得脚本配置文件物理路径
        /// </summary>
        /// <returns>返回物流路径字符串</returns>
        private string GetXmlPhysicalApplicationPath() {
         string rawCurrentPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
         string strXmlPath = rawCurrentPath + "reference.xml";
         return strXmlPath;
        }

       }
      }
     }
    }


    脚本配置文件如下:
    <?xml version="1.0" encoding="utf-8" ?>
    <javaScript-reference>
     <!-- 脚本文件对应的名称以及路径 -->
     <file-name name="label" src="js\Label.js"></file-name>
     <file-name name="input" src="js\Input.js"></file-name>
     <file-name name="btnTextInput" src="js\BtnTextInput.js"></file-name>
     <file-name name="table" src="js\Table.js"></file-name>
     <file-name name="ControlUtils" src="js\ControlUtils.js"></file-name>
     <file-name name="Utils" src="js\Utils.js"></file-name>
     <file-name name="WebUtils" src="js\WebUtils.js"></file-name>
     <!-- 脚本关联文件 -->
     <file-reference name="input">
      <file-reference-name name="ControlUtils"></file-reference-name>
      <file-reference-name name="WebUtils"></file-reference-name>
     </file-reference>
     <file-reference name="table">
      <file-reference-name name="ControlUtils"></file-reference-name>
      <file-reference-name name="Utils"></file-reference-name>
      <file-reference-name name="WebUtils"></file-reference-name>
     </file-reference>
     <!--控件-->
     <component class="Prolink.Web.UI.Label" reference-file="label" init-method="installLableDefaultListener"></component>
     <component class="Prolink.Web.UI.TextBox" reference-file="input" init-method="installInputDefaultListener"></component>
     <component class="ProlinkControl.ProlinkBtnTextInput" reference-file="btnTextInput" init-method="installBtnTextInputDefaultListener"></component>
     <component class="Prolink.Web.UI.Table" reference-file="table" init-method="installTableDefaultListener"></component>
    </javaScript-reference>


    WebForm1.aspx.cs:

     public class WebForm1 : Prolink.Web.UI.WebForm
     {
     
      private void Page_Load(object sender, System.EventArgs e)
      {
       // 在此处放置用户代码以初始化页面
      }

  • 相关阅读:
    在 Windows 10 中创建任何大小的虚拟测试文件的 2 种方法
    最近的github又不稳了。。ip host 大法来
    windows mklink /d /h /j 精讲
    kafka-manager配置和使用
    Java——七种垃圾收集器+JDK11最新ZGC
    聚簇索引和非聚簇索引(通俗易懂 言简意赅)
    【转载】Java中的锁机制 synchronized & 偏向锁 & 轻量级锁 & 重量级锁 & 各自优缺点及场景 & AtomicReference
    都1202年了奉劝那些还在用centos6的gs,赶紧切ubuntu-Centos6 升级 gcc 惨痛教训
    Tamper Chrome – 请求修改扩展,可用于Web安全测试
    线程、线程池三大方法、七大参数、四种策略
  • 原文地址:https://www.cnblogs.com/wtiancai/p/235274.html
Copyright © 2011-2022 走看看