ClientScriptManager.RegisterStartupScript 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將啟動腳本註冊為物件 Page 。
多載
| 名稱 | Description |
|---|---|
| RegisterStartupScript(Type, String, String) |
透過型別、鍵和腳本字面值,將啟動腳本註冊給 Page 物件。 |
| RegisterStartupScript(Type, String, String, Boolean) |
透過型別、鍵、腳本字面值及布林值(指示是否加入腳本標籤)註冊啟動腳本 Page 與物件。 |
RegisterStartupScript(Type, String, String)
透過型別、鍵和腳本字面值,將啟動腳本註冊給 Page 物件。
public:
void RegisterStartupScript(Type ^ type, System::String ^ key, System::String ^ script);
public void RegisterStartupScript(Type type, string key, string script);
member this.RegisterStartupScript : Type * string * string -> unit
Public Sub RegisterStartupScript (type As Type, key As String, script As String)
參數
- type
- Type
啟動腳本要註冊的類型。
- key
- String
啟動腳本的鍵要註冊。
- script
- String
啟動腳本要直接註冊。
範例
以下程式碼範例示範了此 RegisterStartupScript 方法的使用。 請注意,開頭和結尾腳本標籤都包含在參數中 script 。 若要根據額外參數設定新增腳本標籤,請參閱該 RegisterStartupScript 方法。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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";
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))
{
StringBuilder cstext1 = new StringBuilder();
cstext1.Append("<script type=text/javascript> alert('Hello World!') </");
cstext1.Append("script>");
cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>RegisterStartupScript</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Public Sub Page_Load(ByVal sender As [Object], ByVal e As EventArgs)
' Define the name and type of the client scripts on the page.
Dim csname1 As [String] = "PopupScript"
Dim cstype As Type = Me.[GetType]()
' Get a ClientScriptManager reference from the Page class.
Dim cs As ClientScriptManager = Page.ClientScript
' Check to see if the startup script is already registered.
If Not cs.IsStartupScriptRegistered(cstype, csname1) Then
Dim cstext1 As New StringBuilder()
cstext1.Append("<script type=text/javascript> alert('Hello World!') </")
cstext1.Append("script>")
cs.RegisterStartupScript(cstype, csname1, cstext1.ToString())
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>RegisterStartupScript</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
備註
用戶端腳本以其鍵與類型唯一識別。 具有相同鍵與型別的腳本被視為重複。 該頁面只能註冊一個具有特定類型與金鑰對的腳本。 嘗試註冊已註冊的腳本不會產生該腳本的重複。
呼叫該 IsStartupScriptRegistered 方法來判斷具有特定金鑰與型態對的啟動腳本是否已註冊,並避免不必要的嘗試新增該腳本。
在這種方法過載 RegisterStartupScript 時,你必須確保參數中 script 提供的腳本被元素區塊包裹 <script> 。
該方法新增 RegisterStartupScript 的腳本區塊會在頁面載入完成但頁面事件被觸發前 OnLoad 執行。 腳本區塊不保證會依照註冊順序輸出。 如果腳本區塊的順序很重要,可以用 StringBuilder 物件把這些腳本集合成一個字串,然後全部註冊在一個客戶端腳本區塊裡。
另請參閱
適用於
RegisterStartupScript(Type, String, String, Boolean)
透過型別、鍵、腳本字面值及布林值(指示是否加入腳本標籤)註冊啟動腳本 Page 與物件。
public:
void RegisterStartupScript(Type ^ type, System::String ^ key, System::String ^ script, bool addScriptTags);
public void RegisterStartupScript(Type type, string key, string script, bool addScriptTags);
member this.RegisterStartupScript : Type * string * string * bool -> unit
Public Sub RegisterStartupScript (type As Type, key As String, script As String, addScriptTags As Boolean)
參數
- type
- Type
啟動腳本要註冊的類型。
- key
- String
啟動腳本的鍵要註冊。
- script
- String
啟動腳本要直接註冊。
- addScriptTags
- Boolean
一個布林值,表示是否要加入腳本標籤。
例外狀況
type 是 null。
範例
以下程式碼範例示範了此 RegisterStartupScript 方法的使用。 請注意,參數 addScriptTags 設定為 , false 因此開始和結尾腳本標籤都包含在參數中 script 。
<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
<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>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Define the name and type of the client scripts on the page.
Dim csname1 As String = "PopupScript"
Dim csname2 As String = "ButtonClickScript"
Dim cstype As Type = Me.GetType()
' Get a ClientScriptManager reference from the Page class.
Dim cs As ClientScriptManager = Page.ClientScript
' Check to see if the startup script is already registered.
If (Not cs.IsStartupScriptRegistered(cstype, csname1)) Then
Dim cstext1 As String = "alert('Hello World');"
cs.RegisterStartupScript(cstype, csname1, cstext1, True)
End If
' Check to see if the client script is already registered.
If (Not cs.IsClientScriptBlockRegistered(cstype, csname2)) Then
Dim cstext2 As 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)
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<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>
備註
啟動腳本以其金鑰和類型唯一識別。 具有相同鍵與型別的腳本被視為重複。 該頁面只能註冊一個具有特定類型與金鑰對的腳本。 嘗試註冊已註冊的腳本不會產生該腳本的重複。
呼叫該 IsStartupScriptRegistered 方法來判斷具有特定金鑰與型態對的啟動腳本是否已註冊,並避免不必要的嘗試新增該腳本。
在這種方法過RegisterStartupScript載時,你可以用script參數表示參數中提供的<script>腳本是否被元素區塊包裹addScriptTags。 設定 addScriptTags 為 true 表示腳本標籤會自動被加入。
該方法新增 RegisterStartupScript 的腳本區塊會在頁面載入完成但頁面事件被觸發前 OnLoad 執行。 腳本區塊不保證會依照註冊順序輸出。 如果腳本區塊的順序很重要,可以用 StringBuilder 物件把這些腳本集合成一個字串,然後全部註冊在一個客戶端腳本區塊裡。