ClientScriptManager.RegisterClientScriptBlock 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將用戶端腳本註冊為物件 Page 。
多載
| 名稱 | Description |
|---|---|
| RegisterClientScriptBlock(Type, String, String) |
透過型別、鍵和腳本的字面值,將客戶端腳本與 Page 物件註冊。 |
| RegisterClientScriptBlock(Type, String, String, Boolean) |
透過型別、鍵、腳本字面值及布林值將用戶端腳本註冊為物件, Page 指示是否加入腳本標籤。 |
RegisterClientScriptBlock(Type, String, String)
透過型別、鍵和腳本的字面值,將客戶端腳本與 Page 物件註冊。
public:
void RegisterClientScriptBlock(Type ^ type, System::String ^ key, System::String ^ script);
public void RegisterClientScriptBlock(Type type, string key, string script);
member this.RegisterClientScriptBlock : Type * string * string -> unit
Public Sub RegisterClientScriptBlock (type As Type, key As String, script As String)
參數
- type
- Type
要註冊的客戶端腳本類型。
- key
- String
客戶端腳本的金鑰來註冊。
- script
- String
客戶端腳本直接註冊。
範例
以下程式碼範例示範了此 RegisterClientScriptBlock 方法的使用。
<%@ 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 script on the page.
String csName = "ButtonClickScript";
Type csType = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(csType, csName))
{
StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\"> function DoClick() {");
csText.Append("Form1.Message.value='Text from client script.'; }");
csText.Append("</script>");
cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
}
}
</script>
<html >
<head>
<title>RegisterClientScriptBlock 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">
Public Sub Page_Load(ByVal sender As [Object], ByVal e As EventArgs)
' Define the name and type of the client script on the page.
Dim csName 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 client script is already registered.
If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
Dim csText As New StringBuilder()
csText.Append("<script type=""text/javascript""> function DoClick() {")
csText.Append("Form1.Message.value='Text from client script.'; }")
csText.Append("</script>")
cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
End If
End Sub
</script>
<html >
<head>
<title>RegisterClientScriptBlock Example</title>
</head>
<body>
<form id="Form1"
runat="server">
<input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
</form>
</body>
</html>
備註
用戶端腳本以其鍵與類型唯一識別。 具有相同鍵與型別的腳本被視為重複。 該頁面只能註冊一個具有特定類型與金鑰對的腳本。 嘗試註冊已註冊的腳本不會產生該腳本的重複。
呼叫該 IsClientScriptBlockRegistered 方法來判斷具有特定金鑰與類型配對的用戶端腳本是否已被註冊,避免不必要的嘗試新增該腳本。
在這種方法過 RegisterClientScriptBlock 載中,你必須確保參數中 script 提供的腳本被元素區塊包裹 <script> 。
此 RegisterClientScriptBlock 方法會在渲染頁面頂端新增一個腳本區塊。 腳本區塊不保證會依照註冊順序輸出。 如果腳本區塊的順序很重要,可以用 StringBuilder 物件把這些腳本集合成一個字串,然後全部註冊在一個客戶端腳本區塊裡。
另請參閱
適用於
RegisterClientScriptBlock(Type, String, String, Boolean)
透過型別、鍵、腳本字面值及布林值將用戶端腳本註冊為物件, Page 指示是否加入腳本標籤。
public:
void RegisterClientScriptBlock(Type ^ type, System::String ^ key, System::String ^ script, bool addScriptTags);
public void RegisterClientScriptBlock(Type type, string key, string script, bool addScriptTags);
member this.RegisterClientScriptBlock : Type * string * string * bool -> unit
Public Sub RegisterClientScriptBlock (type As Type, key As String, script As String, addScriptTags As Boolean)
參數
- type
- Type
要註冊的客戶端腳本類型。
- key
- String
客戶端腳本的金鑰來註冊。
- script
- String
客戶端腳本直接註冊。
- addScriptTags
- Boolean
一個布林值,表示是否要加入腳本標籤。
例外狀況
用戶端腳本區塊類型為 null。
範例
以下程式碼範例示範了此 RegisterClientScriptBlock 方法的使用。 請注意,參數 addScriptTags 設定 true 為 ,因此開頭和結尾腳本標籤不會隨參數包含 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>
備註
用戶端腳本以其鍵與類型唯一識別。 具有相同鍵與型別的腳本被視為重複。 該頁面只能註冊一個具有特定類型與金鑰對的腳本。 嘗試註冊已註冊的腳本不會產生該腳本的重複。
呼叫該 IsClientScriptBlockRegistered 方法來判斷具有特定金鑰與型態對的用戶端腳本是否已被註冊。 這樣可以避免不必要的嘗試加入腳本。
在這種方法過RegisterClientScriptBlock載時,你可以用addScriptTags參數表示參數中提供的script腳本是否被元素區塊包裹<script>。 設定 addScriptTags 為 true 表示腳本標籤會自動被加入。
此 RegisterClientScriptBlock 方法會在渲染頁面頂端新增一個腳本區塊。 腳本區塊不保證會依照註冊順序輸出。 如果腳本區塊的順序很重要,可以用 StringBuilder 物件把這些腳本集合成一個字串,然後全部註冊在一個客戶端腳本區塊裡。