【wtiancai 】asp.net註冊指令碼塊

iDotNetSpace發表於2008-06-16
自定義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);//--&gt");
     }
     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 + ");//--&gt";
       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;
    }

   }
  }
 }
}


指令碼配置檔案如下:


 <!-- 指令碼檔案對應的名稱以及路徑 --&gt
 
 
 
 
 
 
 
 <!-- 指令碼關聯檔案 --&gt
 
  
  
 

 
  
  
  
 

 <!--控制元件--&gt
 
 
 
 



WebForm1.aspx.cs:

 public class WebForm1 : Prolink.Web.UI.WebForm
 {
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此處放置使用者程式碼以初始化頁面
  }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-349319/,如需轉載,請註明出處,否則將追究法律責任。

相關文章