ClientScriptManager.RegisterClientScriptInclude ClientScriptManager.RegisterClientScriptInclude ClientScriptManager.RegisterClientScriptInclude ClientScriptManager.RegisterClientScriptInclude Method

定義

Page 物件註冊用戶端指令碼 Include。Registers the client script include with the Page object.

多載

RegisterClientScriptInclude(String, String) RegisterClientScriptInclude(String, String) RegisterClientScriptInclude(String, String) RegisterClientScriptInclude(String, String)

使用索引鍵和 URL 在 Page 物件註冊用戶端指令碼,讓您能夠從用戶端呼叫此指令碼。Registers the client script with the Page object using a key and a URL, which enables the script to be called from the client.

RegisterClientScriptInclude(Type, String, String) RegisterClientScriptInclude(Type, String, String) RegisterClientScriptInclude(Type, String, String)

使用型別、索引鍵和 URL,向 Page 物件註冊用戶端指令碼 Include。Registers the client script include with the Page object using a type, a key, and a URL.

RegisterClientScriptInclude(String, String) RegisterClientScriptInclude(String, String) RegisterClientScriptInclude(String, String) RegisterClientScriptInclude(String, String)

使用索引鍵和 URL 在 Page 物件註冊用戶端指令碼,讓您能夠從用戶端呼叫此指令碼。Registers the client script with the Page object using a key and a URL, which enables the script to be called from the client.

public:
 void RegisterClientScriptInclude(System::String ^ key, System::String ^ url);
public void RegisterClientScriptInclude (string key, string url);
member this.RegisterClientScriptInclude : string * string -> unit
Public Sub RegisterClientScriptInclude (key As String, url As String)

參數

key
String String String String

要註冊的用戶端指令碼 Include 索引鍵。The key of the client script include to register.

url
String String String String

要註冊的用戶端指令碼 Include URL。The URL of the client script include to register.

範例

如需相關資訊, 包括語法、使用方式和範例, 請RegisterClientScriptInclude參閱。For related information, including syntax, usage, and an example, see RegisterClientScriptInclude.

備註

用戶端腳本包含依其索引鍵及其類型來唯一識別。A client script include is uniquely identified by its key and its type. 具有相同索引鍵和類型的腳本會視為重複。Scripts with the same key and type are considered duplicates. 只有一個具有指定類型和金鑰組的腳本可以向頁面註冊。Only one script with a given type and key pair can be registered with the page. 嘗試註冊已註冊的腳本並不會建立腳本的複本。Attempting to register a script that is already registered does not create a duplicate of the script.

IsClientScriptIncludeRegistered呼叫方法, 以判斷用戶端腳本是否包含指定的索引鍵, 且類型組是否已註冊, 並避免不必要地嘗試加入腳本。Call the IsClientScriptIncludeRegistered method to determine whether a client script include with a given key and type pair is already registered and avoid unnecessarily attempting to add the script.

注意

若要解析用戶端 URL, 請ResolveClientUrl使用方法。To resolve the client URL, use the ResolveClientUrl method. 這個方法會使用呼叫它的 URL 內容來解析路徑。This method uses the context of the URL on which it is called to resolve the path.

此方法的多載key會呼叫接受typeURL和參數的多載。 RegisterClientScriptIncludeThis overload of the RegisterClientScriptInclude method calls the overload that takes a key, a URL, and a type parameter.

方法會將腳本區塊加入轉譯頁面的頂端。The method adds a script block at the top of the rendered page.

另請參閱

RegisterClientScriptInclude(Type, String, String) RegisterClientScriptInclude(Type, String, String) RegisterClientScriptInclude(Type, String, String)

使用型別、索引鍵和 URL,向 Page 物件註冊用戶端指令碼 Include。Registers the client script include with the Page object using a type, a key, and a URL.

public:
 void RegisterClientScriptInclude(Type ^ type, System::String ^ key, System::String ^ url);
public void RegisterClientScriptInclude (Type type, string key, string url);
member this.RegisterClientScriptInclude : Type * string * string -> unit

參數

type
Type Type Type Type

要註冊的用戶端指令碼 Include 型別。The type of the client script include to register.

key
String String String String

要註冊的用戶端指令碼 Include 索引鍵。The key of the client script include to register.

url
String String String String

要註冊的用戶端指令碼 Include URL。The URL of the client script include to register.

例外狀況

用戶端指令碼 Include 型別為 nullThe client script include type is null.

URL 為 nullThe URL is null.

-或--or- URL 是空的。The URL is empty.

範例

下列程式碼範例示範如何使用RegisterClientScriptInclude方法。The following code example demonstrates the use of the RegisterClientScriptInclude method. 請注意, 如果要檢查現有用戶端腳本的邏輯是否已移除, 則轉譯的頁面中仍然不會有重複的用戶端腳本RegisterClientScriptInclude , 因為方法會檢查是否有重複專案。Note that if the logic to check for the existing client script include were removed, there would still not be duplicate client scripts in the rendered page because the RegisterClientScriptInclude method checks for duplicates. 檢查的優點是減少不必要的計算。The benefit of checking is to reduce unnecessary computation.

<%@ 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 的 JavaScript 檔案, 其中包含下列內容:This example requires a JavaScript file named Script_include.js with the following contents:

function DoClick() {Form1.Message.value='Text from include script.'}  

備註

此方法的多載會採用keyurl參數來識別type腳本, 以及指定用戶端腳本識別的參數 (包含)。 RegisterClientScriptIncludeThis overload of the RegisterClientScriptInclude method takes key and url parameters to identify the script, as well as a type parameter to specify the identification of the client script include. 您可以根據將要存取資源的物件來指定類型。You specify the type based on the object that will be accessing the resource. 例如, 當使用Page實例來存取資源時, 您可以Page指定類型。For instance, when using a Page instance to access the resource, you specify the Page type.

注意

若要解析用戶端 URL, 請ResolveClientUrl使用方法。To resolve the client URL, use the ResolveClientUrl method. 這個方法會使用呼叫它的 URL 內容來解析路徑。This method uses the context of the URL on which it is called to resolve the path.

這個方法會將腳本區塊加入轉譯頁面的頂端。This method adds a script block at the top of the rendered page.

另請參閱

適用於