ClientScriptManager.IsClientScriptBlockRegistered 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
判斷用戶端腳本區塊是否已註冊於該 Page 物件。
多載
| 名稱 | Description |
|---|---|
| IsClientScriptBlockRegistered(String) |
判斷用戶端腳本區塊是否已以指定金鑰註冊於物件 Page 。 |
| IsClientScriptBlockRegistered(Type, String) |
判斷用戶端腳本區塊是否以金鑰與類型與物件註冊 Page 。 |
IsClientScriptBlockRegistered(String)
判斷用戶端腳本區塊是否已以指定金鑰註冊於物件 Page 。
public:
bool IsClientScriptBlockRegistered(System::String ^ key);
public bool IsClientScriptBlockRegistered(string key);
member this.IsClientScriptBlockRegistered : string -> bool
Public Function IsClientScriptBlockRegistered (key As String) As Boolean
參數
- key
- String
要搜尋的客戶腳本區塊的金鑰。
傳回
true如果用戶端腳本區塊被註冊;否則,。 false
範例
<%@ 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);
}
// 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());
}
}
</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)
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())
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>
備註
在呼叫 RegisterClientScriptBlock 該方法之前,先呼叫此方法,以避免註冊重複腳本。 如果腳本需要大量伺服器資源來建立,這點尤其重要。
用戶端腳本以其鍵與類型唯一識別。 具有相同鍵與型別的腳本被視為重複。
此方法的超載 IsClientScriptBlockRegistered 會呼叫一個同時取 a key 與 type 參數的超載,且該 Page 參數設為物件型別
另請參閱
適用於
IsClientScriptBlockRegistered(Type, String)
判斷用戶端腳本區塊是否以金鑰與類型與物件註冊 Page 。
public:
bool IsClientScriptBlockRegistered(Type ^ type, System::String ^ key);
public bool IsClientScriptBlockRegistered(Type type, string key);
member this.IsClientScriptBlockRegistered : Type * string -> bool
Public Function IsClientScriptBlockRegistered (type As Type, key As String) As Boolean
參數
- type
- Type
要搜尋的客戶端腳本區塊類型。
- key
- String
要搜尋的客戶腳本區塊的金鑰。
傳回
true如果用戶端腳本區塊被註冊;否則,。 false
例外狀況
用戶端腳本類型為 null。
範例
以下程式碼範例示範了此 IsClientScriptBlockRegistered 方法的使用。 請注意,如果移除檢查現有客戶端腳本區塊的邏輯,渲染頁面的 HTML 原始碼中就不會有兩個重複的客戶端腳本,因為該 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 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>
備註
在呼叫 RegisterClientScriptBlock 該方法之前,先呼叫此方法,以避免註冊重複腳本。 如果腳本需要大量伺服器資源來建立,這點尤其重要。
用戶端腳本以其鍵與類型唯一識別。 具有相同鍵與型別的腳本被視為重複。 你可以根據將要存取資源的物件來指定類型。 例如,當使用 Page 實例存取資源時,你指定 Page 了類型。