ClientScriptManager.IsStartupScriptRegistered 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
判斷啟始指令碼是否向 Page 物件註冊過。
多載
IsStartupScriptRegistered(Type, String) |
判斷啟始指令碼是否已使用指定的索引鍵和型別,向 Page 物件註冊。 |
IsStartupScriptRegistered(String) |
判斷啟始指令碼是否已使用指定的索引鍵,向 Page 物件註冊。 |
IsStartupScriptRegistered(Type, String)
判斷啟始指令碼是否已使用指定的索引鍵和型別,向 Page 物件註冊。
public:
bool IsStartupScriptRegistered(Type ^ type, System::String ^ key);
public bool IsStartupScriptRegistered (Type type, string key);
member this.IsStartupScriptRegistered : Type * string -> bool
Public Function IsStartupScriptRegistered (type As Type, key As String) As Boolean
參數
- type
- Type
要搜尋的啟始指令碼型別。
- key
- String
要搜尋的啟始指令碼索引鍵。
傳回
如果啟始指令碼已登錄,則為 true
,否則為 false
。
例外狀況
啟始指令碼型別為 null
。
範例
下列程式碼範例示範 如何使用 IsStartupScriptRegistered 方法。 請注意,如果移除了檢查現有啟動腳本區塊的邏輯,則轉譯頁面的 HTML 原始程式碼中不會有兩個重複的啟動腳本,因為 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";
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>
備註
呼叫 方法之前先呼叫 RegisterStartupScript 這個方法,以避免註冊重複的腳本。 如果腳本需要大量伺服器資源才能建立,這特別重要。
用戶端啟動腳本是由其索引鍵和其類型唯一識別。 具有相同索引鍵和類型的腳本會被視為重複專案。
另請參閱
適用於
IsStartupScriptRegistered(String)
判斷啟始指令碼是否已使用指定的索引鍵,向 Page 物件註冊。
public:
bool IsStartupScriptRegistered(System::String ^ key);
public bool IsStartupScriptRegistered (string key);
member this.IsStartupScriptRegistered : string -> bool
Public Function IsStartupScriptRegistered (key As String) As Boolean
參數
- key
- String
要搜尋的啟始指令碼索引鍵。
傳回
如果啟始指令碼已登錄,則為 true
,否則為 false
。
備註
呼叫 方法之前先呼叫 RegisterStartupScript 這個方法,以避免註冊重複的腳本。 如果腳本需要大量伺服器資源才能建立,這特別重要。
啟動腳本是由其索引鍵和其類型唯一識別。 具有相同索引鍵和類型的腳本會被視為重複專案。
此方法的 IsStartupScriptRegistered 這個多載會呼叫多載,該多載會同時接受字串 key
和 type
參數,並將類型設定為 Page 物件