ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 【wtiancai 】asp.net注册脚本块

【wtiancai 】asp.net注册脚本块

原创 Linux操作系统 作者:iDotNetSpace 时间:2008-06-16 10:20:44 0 删除 编辑
自定义WebForm类,继承Page,从而可以在aspx页面上自动生成脚本包含文件和脚本初始化事件。

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

1。RegisterClientScriptBlock:


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

参数

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

备注

客户端脚本刚好在 Page 对象的 元素的开始标记后发出。脚本块是在呈现输出的对象被定义时发出的,因此必须同时包括 ");
     }
     if (!this.IsStartupScriptRegistered("body")) {
      this.RegisterStartupScript("body","<!--installBodyDefaultListener(document.body);//-->");
     }
     doc.Load(GetXmlPhysicalApplicationPath());
     GetScript(this);
    }

    ///


    /// 注册页面所有脚本块
    ///

    /// 某个控件类
    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. = "";
        this.RegisterClientScriptBlock(fileName,sScript);
       }
      }
      if (!this.IsStartupScriptRegistered(c.ID)) {
       sScript. = "<!--" + initMethod + "(document.all." + c.ID + ");//-->";
       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;
    }

    ///


    /// 获得脚本配置文件物理路径
    ///

    /// 返回物流路径字符串
    private string GetXmlPhysicalApplicationPath() {
     string rawCurrentPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
     string strXmlPath = rawCurrentPath + "reference.xml";
     return strXmlPath;
    }

   }
  }
 }
}


脚本配置文件如下:


 <!-- 脚本文件对应的名称以及路径 -->
 
 
 
 
 
 
 
 <!-- 脚本关联文件 -->
 
  
  
 

 
  
  
  
 

 <!--控件-->
 
 
 
 



WebForm1.aspx.cs:

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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-349319/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5769026