ClientScriptManager.IsClientScriptIncludeRegistered 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
判斷用戶端指令碼 Include 是否向 Page 物件註冊過。
多載
IsClientScriptIncludeRegistered(String) |
判斷用戶端指令碼 Include 是否已使用指定的索引鍵,向 Page 物件註冊。 |
IsClientScriptIncludeRegistered(Type, String) |
判斷用戶端指令碼 Include 是否已使用指定的索引鍵和型別,向 Page 物件註冊。 |
IsClientScriptIncludeRegistered(String)
判斷用戶端指令碼 Include 是否已使用指定的索引鍵,向 Page 物件註冊。
public:
bool IsClientScriptIncludeRegistered(System::String ^ key);
public bool IsClientScriptIncludeRegistered (string key);
member this.IsClientScriptIncludeRegistered : string -> bool
Public Function IsClientScriptIncludeRegistered (key As String) As Boolean
參數
- key
- String
要搜尋的用戶端指令碼 Include 索引鍵。
傳回
如果用戶端指令碼 Include 已註冊,則為 true
,否則為 false
。
備註
呼叫 方法之前先呼叫 RegisterClientScriptInclude 這個方法,以避免註冊重複的腳本。 如果腳本需要大量伺服器資源才能建立,這特別重要。
用戶端腳本包含是由其索引鍵和其類型唯一識別。 具有相同索引鍵和類型的腳本會被視為重複專案。
此方法的 IsStartupScriptRegistered 這個多載會呼叫多載,該多載會 key
採用 和 type
參數,並將類型設定為 Page 物件。
另請參閱
適用於
IsClientScriptIncludeRegistered(Type, String)
判斷用戶端指令碼 Include 是否已使用指定的索引鍵和型別,向 Page 物件註冊。
public:
bool IsClientScriptIncludeRegistered(Type ^ type, System::String ^ key);
public bool IsClientScriptIncludeRegistered (Type type, string key);
member this.IsClientScriptIncludeRegistered : Type * string -> bool
Public Function IsClientScriptIncludeRegistered (type As Type, key As String) As Boolean
參數
- type
- Type
要搜尋的用戶端指令碼 Include 型別。
- key
- String
要搜尋的用戶端指令碼 Include 索引鍵。
傳回
如果用戶端指令碼 Include 已註冊,則為 true
,否則為 false
。
例外狀況
用戶端指令碼 Include 型別為 null
。
範例
下列程式碼範例示範 如何使用 IsClientScriptIncludeRegistered 方法。 請注意,如果移除了檢查現有用戶端腳本的邏輯,則轉譯頁面的 HTML 原始程式碼中不會有兩個重複的用戶端腳本,因為 RegisterClientScriptInclude 方法會檢查重複專案。 檢查的優點是減少不必要的計算。
<%@ 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, type and url of the client script on the page.
String csname = "ButtonClickScript";
String csurl = "~/script_include.js";
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the include script exists already.
if (!cs.IsClientScriptIncludeRegistered(cstype, csname))
{
cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl));
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ClientScriptManager Example</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<input type="text"
id="Message"/>
<input type="button"
value="ClickMe"
onclick="DoClick()"/>
</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">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Define the name, type and url of the client script on the page.
Dim csname As String = "ButtonClickScript"
Dim csurl As String = "~/script_include.js"
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 include script is already registered.
If (Not cs.IsClientScriptIncludeRegistered(cstype, csname)) Then
cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl))
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ClientScriptManager Example</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<input type="text"
id="Message"/>
<input type="button"
value="ClickMe"
onclick="DoClick()"/>
</div>
</form>
</body>
</html>
此範例需要名為 的 Script_include.js
JavaScript 檔案,其中包含下列內容:
function DoClick() {Form1.Message.value='Text from include script.'}
備註
呼叫 方法之前先呼叫 RegisterClientScriptInclude 這個方法,以避免註冊重複的用戶端腳本包含。 如果腳本需要大量伺服器資源才能建立,這特別重要。
用戶端腳本包含是由其索引鍵和其類型唯一識別。 具有相同索引鍵和類型的腳本會被視為重複專案。 您可以根據將存取資源的物件來指定型別。 例如,使用 Page 實例來存取資源時,您可以指定 Page
類型。