使用類型、鍵、腳本文本和指示是否添加腳本標記的布爾值向 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>?
-
??
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

