【wtiancai 】asp.net註冊指令碼塊
先介紹一下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","
}
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. = "
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部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-349319/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Asp.net Ajax 註冊框架ASP.NET框架
- vbs指令碼獲取Am註冊路徑資訊指令碼
- Emeditor 註冊碼
- WebStorm註冊碼WebORM
- 呼叫域名註冊api,查詢所有域名組合指令碼API指令碼
- PhpStorm註冊碼PHPORM
- Navicat for MySQL註冊碼MySql
- SecureCRT 7 註冊碼Securecrt
- 常用指令碼學習手冊——Bat指令碼指令碼BAT
- 註冊中心 Eureka 原始碼解析 —— 應用例項註冊發現(一)之註冊原始碼
- Viscosity for Mac 註冊碼:Mac
- PLSQL Developer 12 註冊碼SQLDeveloper
- PLSQL Developer 9.0註冊碼SQLDeveloper
- sublime text for Mac註冊啟用 sublime text4註冊碼Mac
- Instant Source 註冊演算法分析+註冊器原始碼演算法原始碼
- 雲伺服器IIS 註冊 ASP.NET 2.0/4.0伺服器ASP.NET
- wing ftp server 註冊碼FTPServer
- phpstrom 註冊碼獲取PHP
- IntelliJ IDEA 註冊碼IntelliJIdea
- Pycharm安裝破解 註冊碼PyCharm
- Myeclipse10註冊碼Eclipse
- myeclipse獲取註冊碼Eclipse
- Theme Builder註冊碼分析UI
- ShadowDefender 註冊碼 分析
- Navicat for MySQL 11註冊碼\啟用碼MySql
- JavaScript And Ajax(呈現指令碼塊、指令碼注入攻擊)JavaScript指令碼
- 今天好多人 phpstrom 編譯器註冊碼失效了,最新可用註冊碼PHP編譯
- Asp.Net Core入門之自定義服務註冊ASP.NET
- Oracle sql指令碼中註釋OracleSQL指令碼
- 區塊鏈為何要註冊新加坡基金會區塊鏈
- 分享一個自動傳輸歸檔到備庫並註冊的指令碼指令碼
- nacos註冊中心原始碼流程分析原始碼
- IntelliJ IDEA 14 註冊碼IntelliJIdea
- pycharm 2016.3.2註冊碼PyCharm
- Pycharm 2016註冊碼PyCharm
- PLSQL Developer V9 註冊碼SQLDeveloper
- 動態註冊和靜態註冊
- 靜態註冊和動態註冊