使用類型、鍵、腳本文本和指示是否添加腳本標記的布爾值向 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>
-
