Page.ClientScript.RegisterStartupScript用法小結

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

使用類型、鍵、指令碼文本和指示是否添加指令碼標記的布爾值向 Page 對象註冊啟動指令碼。

參數

type

要註冊的啟動指令碼的類型。

key

要註冊的啟動指令碼的鍵。

script

要註冊的啟動指令碼文本。

addScriptTags

指示是否添加指令碼標記的布爾值.

備忘:

啟動指令碼由它的鍵和類型唯一標識。具有相同的鍵和類型的指令碼被視為重複指令碼。只有使用給定的類型和鍵對的指令碼才能使用該頁面進行註冊。試圖註冊一個已經註冊的指令碼不會建立重複的指令碼。

調用 IsStartupScriptRegistered 方法以確定具有給定的鍵和類型對的啟動指令碼是否已經註冊,從而避免不必要的添加指令碼嘗試。

在 RegisterStartupScript 方法的此重載中,使用 addScriptTags 參數可指示script 參數中提供的指令碼是否封裝在 <script> 元素塊中。將addScriptTags 設定為 true 指示指令碼標記將自動添加。

RegisterStartupScript 方法添加的指令碼塊在頁面載入完成但頁面的 OnLoad 事件引發之前執行。

 

樣本
  <%@ Page Language="C#"%><script runat="server">public void Page_Load(Object sender, EventArgs e){// Define the name and type of the client scripts on the page.String csname1 = "PopupScript";String csname2 = "ButtonClickScript";Type cstype = this.GetType();// Get a ClientScriptManager reference from the Page class.ClientScriptManager cs = Page.ClientScript;// Check to see if the startup script is already registered.if (!cs.IsStartupScriptRegistered(cstype, csname1)){String cstext1 = "alert(‘Hello World‘);";cs.RegisterStartupScript(cstype, csname1, cstext1, true);}// Check to see if the client script is already registered.if (!cs.IsClientScriptBlockRegistered(cstype, csname2)){StringBuilder cstext2 = new StringBuilder();cstext2.Append("<script type=text/javascript> function DoClick() {");cstext2.Append("Form1.Message.value=‘Text from client script.‘} </");cstext2.Append("script>");cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);}}</script><html><head><title>ClientScriptManager Example</title></head><body><form id="Form1"runat="server"><input type="text" id="Message"> <input type="button" value="ClickMe" onclick="DoClick()"></form></body></html>  //ASP.NET後台頁面跳轉  Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>if(confirm(‘儲存成功!是否繼續添加?‘)){location.href=‘ProductonAdd.aspx‘}else{location.href=‘ProductonList.aspx‘}</script>");//後台彈出確定框ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert(‘請正確輸入!‘);</script>");//ASP.NET後台頁面跳轉Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert(‘資料添加成功!‘);{location.href=‘ProductonList.aspx‘}</script>");或Page.ClientScript.RegisterStartupScript(typeof(string), "", "<script>window.location.href=‘AdminMain.aspx‘;</script>");//後台彈出文字框ScriptManager.RegisterStartupScript(Page, typeof(string), "popUp", "window.open(‘rptView.aspx‘,‘預覽列印‘,‘toolbar=no,location=no,scrollbars=yes,top=200px,left=200px,width=904px,height=650px‘)", true); 小技巧: 後台:  protected void Button1_Click(object sender, EventArgs e)        {            string temp = Request["he"].ToString();//通過request[控制項name(非id)]來引用前台的input控制項            Page.ClientScript.RegisterStartupScript(this.GetType(), "js", "alert(‘hello" + temp + "‘)", true);//alert添加參數         }前台:<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">       <fieldset>    <legend>    健康資訊    </legend>    高度:<input name="he" type="text"/>    體重: <input type="text" />    </fieldset>    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />       </form></body></html>

 

 

 

Page.ClientScript.RegisterStartupScript用法小結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.