ClientScriptManager.IsClientScriptIncludeRegistered 方法

定義

判斷用戶端腳本是否已註冊於物件 Page

多載

名稱 Description
IsClientScriptIncludeRegistered(String)

判斷用戶端腳本 include 是否已透過指定鍵與物件註冊 Page

IsClientScriptIncludeRegistered(Type, String)

判斷用戶端腳本是否包含在物件中,使用鍵與型別註冊 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

客戶腳本的關鍵字包括搜尋。

傳回

true如果客戶端腳本 include 已註冊;否則,。 false

備註

在呼叫 RegisterClientScriptInclude 該方法之前,先呼叫此方法,以避免註冊重複腳本。 如果腳本需要大量伺服器資源來建立,這點尤其重要。

用戶端腳本包含以其鍵與類型唯一識別。 具有相同鍵與型別的腳本被視為重複。

此方法的過載 IsStartupScriptRegistered 呼叫了同時取 a key 與參數 type 且型別設為 Page 物件的超載。

另請參閱

適用於

IsClientScriptIncludeRegistered(Type, String)

判斷用戶端腳本是否包含在物件中,使用鍵與型別註冊 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

客戶腳本的類型包含搜尋內容。

key
String

客戶腳本的關鍵字包括搜尋。

傳回

true如果客戶端腳本 include 已註冊;否則,。 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

另請參閱

適用於