RegisterClientScriptBlock 與 RegisterStartupScript 詳細用法

來源:互聯網
上載者:User

RegisterClientScriptBlock
與 RegisterStartupScript
詳細用法

 

雖然,Response.Write 可以輸出 JavaScript,但輸出的內容是在 <html> 之前,這會導致一些問題,比如樣式表失效,甚至會導致一些脆弱的瀏覽器發生錯誤。

 

ClientScript 則可以方便地管理 JavaScript,

應該說
ClientScript.RegisterClientScriptBlock 與
ClientScript.RegisterStartupScript 只有一點區別,

那就是
RegisterClientScriptBlock 將指令碼代碼寫在 <form> 之後,


RegisterStartupScript 將代碼寫在 </form>(注意是結束標籤)之前。

 

public void RegisterClientScriptBlock(Type type, string key, string script)
public void RegisterClientScriptBlock(Type type, string key, string script, bool addScriptTags)public void RegisterStartupScript(Type type, string key, string script)
public void RegisterStartupScript(Type type, string key, string script, bool addScriptTags)

可以看出二者文法相同。

  • type
    要註冊的啟動指令碼的類型。
  • key
    要註冊的啟動指令碼的鍵,也就是你自己給這段指令碼起的名字。相同 key 的指令碼被當作是重複的,對於這樣的指令碼只輸出最先註冊的,ClientScriptBlock 和 StartupScript 中的 key 相同不算是重複的。
  • script
    指令碼代碼。
  • addScriptTags
    是否添加 <script> 標籤,如果指令碼代碼中不含 <script> 標籤,則應該指定該值為 true,若不指定該值,會被當作 false 對待。
調用樣本:ClientScript.RegisterClientScriptBlock(this.GetType(), "key1", @"function Go(){  alert('');}", true);ClientScript.RegisterStartupScript(this.GetType(), "key1", "Go();", true);原帖地址:http://www.cftea.com/c/2008/10/RBDBXUZAVHWAYFLO.asp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.