Share via


ClientScriptManager.RegisterClientScriptInclude 方法

定義

Page 物件註冊用戶端指令碼 Include。

多載

RegisterClientScriptInclude(String, String)

使用索引鍵和 URL 在 Page 物件註冊用戶端指令碼,讓您能夠從用戶端呼叫此指令碼。

RegisterClientScriptInclude(Type, String, String)

使用型別、索引鍵和 URL,向 Page 物件註冊用戶端指令碼 Include。

RegisterClientScriptInclude(String, String)

使用索引鍵和 URL 在 Page 物件註冊用戶端指令碼,讓您能夠從用戶端呼叫此指令碼。

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

要註冊的用戶端指令碼 Include 索引鍵。

url
String

要註冊的用戶端指令碼 Include URL。

範例

如需相關資訊,包括語法、使用方式和範例,請參閱 RegisterClientScriptInclude

備註

用戶端腳本 include 是由其索引鍵和其類型唯一識別。 具有相同索引鍵和類型的腳本會被視為重複專案。 只有一個具有指定類型和金鑰組的腳本可以向頁面註冊。 嘗試註冊已註冊的腳本並不會建立腳本的重複專案。

IsClientScriptIncludeRegistered呼叫 方法來判斷用戶端腳本是否已經註冊具有指定索引鍵和類型組的用戶端腳本,並避免不必要地嘗試新增腳本。

注意

若要解析用戶端 URL,請使用 ResolveClientUrl 方法。 這個方法會使用呼叫 URL 的內容來解析路徑。

這個 方法的多 RegisterClientScriptInclude 載會呼叫採用 keyURLtype 參數的多載。

方法會在轉譯頁面頂端新增腳本區塊。

另請參閱

適用於

RegisterClientScriptInclude(Type, String, String)

使用型別、索引鍵和 URL,向 Page 物件註冊用戶端指令碼 Include。

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
Public Sub RegisterClientScriptInclude (type As Type, key As String, url As String)

參數

type
Type

要註冊的用戶端指令碼 Include 型別。

key
String

要註冊的用戶端指令碼 Include 索引鍵。

url
String

要註冊的用戶端指令碼 Include URL。

例外狀況

用戶端指令碼 Include 型別為 null

URL 為 null

-或- URL 是空的。

範例

下列程式碼範例示範 如何使用 RegisterClientScriptInclude 方法。 請注意,如果已移除檢查現有用戶端腳本包含的邏輯,則轉譯頁面中仍然不會有重複的用戶端腳本,因為 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 載會採用 金鑰URL 參數來識別腳本,以及 type 指定用戶端腳本包含之識別的參數。 您可以根據將存取資源的物件來指定型別。 例如,使用 Page 實例來存取資源時,您可以指定 Page 類型。

注意

若要解析用戶端 URL,請使用 ResolveClientUrl 方法。 這個方法會使用呼叫 URL 的內容來解析路徑。

這個方法會在轉譯頁面頂端新增腳本區塊。

另請參閱

適用於