ClientScriptManager ClientScriptManager ClientScriptManager ClientScriptManager Class

定義

定義用來管理 Web 應用程式中用戶端指令碼的方法。Defines methods for managing client scripts in Web applications.

public ref class ClientScriptManager sealed
public sealed class ClientScriptManager
type ClientScriptManager = class
Public NotInheritable Class ClientScriptManager
繼承
ClientScriptManagerClientScriptManagerClientScriptManagerClientScriptManager

範例

下列程式碼範例示範如何使用RegisterClientScriptBlock方法的ClientScriptManager類別。The following code example demonstrates the use of the RegisterClientScriptBlock method of the ClientScriptManager class. 兩個用戶端指令碼會定義於頁面中: PopupScript,其中會顯示警示訊息,載入頁面時,並ButtonClickScript,其定義的 HTML 按鈕的用戶端處理常式onClick事件。Two client scripts are defined in the page: PopupScript, which displays an alert message when the page is loaded, and ButtonClickScript, which defines a client handler for an HTML button's onClick event.

<%@ 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>

備註

ClientScriptManager類別用來管理用戶端指令碼,並將它們新增至 Web 應用程式。The ClientScriptManager class is used to manage client scripts and add them to Web applications. 您可以取得參考ClientScriptManager類別從ClientScript屬性Page物件。You can get a reference to the ClientScriptManager class from the ClientScript property of the Page object.

您可以新增用戶端指令碼至網頁以宣告方式包含在頁面的 HTML 標記中的指令碼。You can add a client script to a Web page declaratively by including the script in the HTML markup of the page. 不過,有些情況需要以動態方式新增用戶端指令碼。However, there are situations when adding client script dynamically is needed. 若要以動態方式加入指令碼,使用RegisterClientScriptBlock方法中,RegisterClientScriptInclude方法,RegisterStartupScript方法,或RegisterOnSubmitStatement方法,會根據時間和方式想要新增指令碼。To add a script dynamically, use the RegisterClientScriptBlock method, the RegisterClientScriptInclude method, the RegisterStartupScript method, or the RegisterOnSubmitStatement method, depending on when and how you want to add the script. 如需詳細資訊,請參閱如何:將用戶端指令碼以動態方式新增至 ASP.NET Web PagesFor more information, see How to: Add Client Script Dynamically to ASP.NET Web Pages.

ClientScriptManager類別唯一識別索引鍵的指令碼StringTypeThe ClientScriptManager class uniquely identifies scripts by a key String and a Type. 具有相同的索引鍵和類型的指令碼會被視為重複。Scripts with the same key and type are considered duplicates. 使用指令碼類型,有助於避免令人困惑的類似指令碼,可能會在頁面上的使用中的不同使用者控制項。Using the script type helps to avoid confusing similar scripts from different user controls that might be in use on the page.

ClientScriptManager類別可以用來叫用用戶端回呼的情況下,最好從用戶端執行伺服端程式碼,而不需執行回傳時。The ClientScriptManager class can be used to invoke client callbacks in situations when it is desirable to run server code from the client without performing a postback. 這被指執行的頻外回呼至伺服器。This is referred to as performing an out-of-band callback to the server. 在用戶端回呼中,用戶端指令碼函式會將非同步要求傳送至 ASP.NET Web 網頁。In a client callback, a client script function sends an asynchronous request to an ASP.NET Web page. 網頁會執行其一般生命週期的修改的版本,以處理回呼。The Web page runs a modified version of its normal life cycle to process the callback. 使用GetCallbackEventReference方法,以取得用戶端函式的參考,叫用時,啟始對伺服器事件的用戶端回呼。Use the GetCallbackEventReference method to obtain a reference to a client function that, when invoked, initiates a client callback to a server event. 如需詳細資訊,請參閱 < 實作用戶端回呼而不需要回傳For more information, see Implementing Client Callbacks Without Postbacks.

注意

指令碼回呼不適用於舊版瀏覽器不支援文件物件模型 (DOM),且需要在用戶端上,會啟用 ECMAScript。Script callbacks will not work in older browsers that do not support the Document Object Model (DOM), and they require that ECMAScript is enabled on the client. 若要檢查瀏覽器是否支援回呼,請使用SupportsCallback屬性,這是可透過存取BrowserASP.NET 內建屬性Request物件。To check if the browser supports callbacks, use the SupportsCallback property, which is accessible through the Browser property of the ASP.NET intrinsic Request object.

使用GetPostBackEventReference方法和GetPostBackClientHyperlink來定義用戶端回傳事件的方法。Use the GetPostBackEventReference method and the GetPostBackClientHyperlink method to define a client postback event. 這些方法可讓用戶端指令碼函式,叫用時,會造成伺服器回傳至頁面。These methods enable client script functions, when invoked, to cause the server to post back to the page. 用戶端回傳事件是用戶端回呼不同之處,在於網頁完成用戶端回傳事件的處理序一般生命週期。A client postback event is different from a client callback in that the Web page completes a normal life cycle to process the client postback event.

注意

如果您使用Button控制項和UseSubmitBehavior屬性設定為false,然後您可以使用GetPostBackEventReference方法,以傳回用戶端回傳事件Button控制項。If you are using a Button control and the UseSubmitBehavior property is set to false, then you can use the GetPostBackEventReference method to return the client postback event for the Button control.

OnClientClick的屬性Button控制ImageButton控制項,和LinkButton控制項可用來執行用戶端指令碼。The OnClientClick property of the Button control, ImageButton control, and LinkButton control can be used to run client script.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetCallbackEventReference(Control, String, String, String) GetCallbackEventReference(Control, String, String, String) GetCallbackEventReference(Control, String, String, String) GetCallbackEventReference(Control, String, String, String)

取得用戶端函式的參考,這個函式會在叫用時,啟始對伺服器事件的用戶端回呼。Obtains a reference to a client function that, when invoked, initiates a client call back to a server event. 這個多載方法的用戶端函式包含指定的控制項、引數、用戶端指令碼和內容。The client function for this overloaded method includes a specified control, argument, client script, and context.

GetCallbackEventReference(Control, String, String, String, Boolean) GetCallbackEventReference(Control, String, String, String, Boolean) GetCallbackEventReference(Control, String, String, String, Boolean) GetCallbackEventReference(Control, String, String, String, Boolean)

取得用戶端函式的參考,這個函式會在叫用時,啟始對伺服器事件的用戶端回呼。Obtains a reference to a client function that, when invoked, initiates a client call back to server events. 這個多載方法的用戶端函式包含指定的控制項、引數、用戶端指令碼、內容和布林值。The client function for this overloaded method includes a specified control, argument, client script, context, and Boolean value.

GetCallbackEventReference(Control, String, String, String, String, Boolean) GetCallbackEventReference(Control, String, String, String, String, Boolean) GetCallbackEventReference(Control, String, String, String, String, Boolean) GetCallbackEventReference(Control, String, String, String, String, Boolean)

取得用戶端函式的參考,這個函式會在叫用時,啟始對伺服器事件的用戶端回呼。Obtains a reference to a client function that, when invoked, initiates a client call back to server events. 這個多載方法的用戶端函式包含指定的控制項、引數、用戶端指令碼、內容、錯誤處理常式和布林值。The client function for this overloaded method includes a specified control, argument, client script, context, error handler, and Boolean value.

GetCallbackEventReference(String, String, String, String, String, Boolean) GetCallbackEventReference(String, String, String, String, String, Boolean) GetCallbackEventReference(String, String, String, String, String, Boolean) GetCallbackEventReference(String, String, String, String, String, Boolean)

取得用戶端函式的參考,這個函式會在叫用時,啟始對伺服器事件的用戶端回呼。Obtains a reference to a client function that, when invoked, initiates a client call back to server events. 這個多載方法的用戶端函式包含指定的目標、引數、用戶端指令碼、內容、錯誤處理常式和布林值。The client function for this overloaded method includes a specified target, argument, client script, context, error handler, and Boolean value.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetPostBackClientHyperlink(Control, String) GetPostBackClientHyperlink(Control, String) GetPostBackClientHyperlink(Control, String) GetPostBackClientHyperlink(Control, String)

取得可用於用戶端事件中的參考,並附加 javascript: 至其開頭,針對指定控制項並以指定事件引數回傳給伺服器。Gets a reference, with javascript: appended to the beginning of it, that can be used in a client event to post back to the server for the specified control and with the specified event arguments.

GetPostBackClientHyperlink(Control, String, Boolean) GetPostBackClientHyperlink(Control, String, Boolean) GetPostBackClientHyperlink(Control, String, Boolean) GetPostBackClientHyperlink(Control, String, Boolean)

取得可用於用戶端事件中的參考 (其開頭附加了 javascript:),針對指定控制項,以指定的事件引數和布林值 (指示是否註冊事件驗證的回傳) 回傳給伺服器。Gets a reference, with javascript: appended to the beginning of it, that can be used in a client event to post back to the server for the specified control with the specified event arguments and Boolean indication whether to register the post back for event validation.

GetPostBackEventReference(Control, String) GetPostBackEventReference(Control, String) GetPostBackEventReference(Control, String) GetPostBackEventReference(Control, String)

傳回字串,該字串可用於用戶端事件中造成回傳給伺服器。Returns a string that can be used in a client event to cause postback to the server. 參考字串是由處理回傳的指定控制項和其他事件資訊的字串引數所定義。The reference string is defined by the specified control that handles the postback and a string argument of additional event information.

GetPostBackEventReference(Control, String, Boolean) GetPostBackEventReference(Control, String, Boolean) GetPostBackEventReference(Control, String, Boolean) GetPostBackEventReference(Control, String, Boolean)

傳回字串,以用於用戶端事件中造成回傳給伺服器。Returns a string to use in a client event to cause postback to the server. 參考字串是由處理回傳的指定控制項和其他事件資訊的字串引數所定義。The reference string is defined by the specified control that handles the postback and a string argument of additional event information. 或者,註冊事件參考以進行驗證。Optionally, registers the event reference for validation.

GetPostBackEventReference(PostBackOptions) GetPostBackEventReference(PostBackOptions) GetPostBackEventReference(PostBackOptions) GetPostBackEventReference(PostBackOptions)

傳回字串,該字串可用於用戶端事件中造成回傳給伺服器。Returns a string that can be used in a client event to cause postback to the server. 指定的 PostBackOptions 執行個體會定義參考字串。The reference string is defined by the specified PostBackOptions instance.

GetPostBackEventReference(PostBackOptions, Boolean) GetPostBackEventReference(PostBackOptions, Boolean) GetPostBackEventReference(PostBackOptions, Boolean) GetPostBackEventReference(PostBackOptions, Boolean)

傳回字串,該字串可用於用戶端事件中造成回傳給伺服器。Returns a string that can be used in a client event to cause postback to the server. 指定的 PostBackOptions 物件會定義參考字串。The reference string is defined by the specified PostBackOptions object. 或者,註冊事件參考以進行驗證。Optionally, registers the event reference for validation.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
GetWebResourceUrl(Type, String) GetWebResourceUrl(Type, String) GetWebResourceUrl(Type, String) GetWebResourceUrl(Type, String)

取得對組件中資源的 URL 參考。Gets a URL reference to a resource in an assembly.

IsClientScriptBlockRegistered(String) IsClientScriptBlockRegistered(String) IsClientScriptBlockRegistered(String) IsClientScriptBlockRegistered(String)

判斷用戶端指令碼區塊是否已使用指定的索引鍵,向 Page 物件註冊。Determines whether the client script block is registered with the Page object using the specified key.

IsClientScriptBlockRegistered(Type, String) IsClientScriptBlockRegistered(Type, String) IsClientScriptBlockRegistered(Type, String) IsClientScriptBlockRegistered(Type, String)

判斷用戶端指令碼區塊是否已使用指定的索引鍵和型別,向 Page 物件註冊。Determines whether the client script block is registered with the Page object using a key and type.

IsClientScriptIncludeRegistered(String) IsClientScriptIncludeRegistered(String) IsClientScriptIncludeRegistered(String) IsClientScriptIncludeRegistered(String)

判斷用戶端指令碼 Include 是否已使用指定的索引鍵,向 Page 物件註冊。Determines whether the client script include is registered with the Page object using the specified key.

IsClientScriptIncludeRegistered(Type, String) IsClientScriptIncludeRegistered(Type, String) IsClientScriptIncludeRegistered(Type, String) IsClientScriptIncludeRegistered(Type, String)

判斷用戶端指令碼 Include 是否已使用指定的索引鍵和型別,向 Page 物件註冊。Determines whether the client script include is registered with the Page object using a key and type.

IsOnSubmitStatementRegistered(String) IsOnSubmitStatementRegistered(String) IsOnSubmitStatementRegistered(String) IsOnSubmitStatementRegistered(String)

判斷 OnSubmit 陳述式是否已使用指定的索引鍵,向 Page 物件註冊。Determines whether the OnSubmit statement is registered with the Page object using the specified key.

IsOnSubmitStatementRegistered(Type, String) IsOnSubmitStatementRegistered(Type, String) IsOnSubmitStatementRegistered(Type, String) IsOnSubmitStatementRegistered(Type, String)

判斷 OnSubmit 陳述式是否已使用指定的索引鍵和類型,向 Page 物件註冊。Determines whether the OnSubmit statement is registered with the Page object using the specified key and type.

IsStartupScriptRegistered(String) IsStartupScriptRegistered(String) IsStartupScriptRegistered(String) IsStartupScriptRegistered(String)

判斷啟始指令碼是否已使用指定的索引鍵,向 Page 物件註冊。Determines whether the startup script is registered with the Page object using the specified key.

IsStartupScriptRegistered(Type, String) IsStartupScriptRegistered(Type, String) IsStartupScriptRegistered(Type, String) IsStartupScriptRegistered(Type, String)

判斷啟始指令碼是否已使用指定的索引鍵和型別,向 Page 物件註冊。Determines whether the startup script is registered with the Page object using the specified key and type.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
RegisterArrayDeclaration(String, String) RegisterArrayDeclaration(String, String) RegisterArrayDeclaration(String, String) RegisterArrayDeclaration(String, String)

使用陣列名稱和陣列值,向 Page 物件註冊 JavaScript 陣列宣告。Registers a JavaScript array declaration with the Page object using an array name and array value.

RegisterClientScriptBlock(Type, String, String) RegisterClientScriptBlock(Type, String, String) RegisterClientScriptBlock(Type, String, String) RegisterClientScriptBlock(Type, String, String)

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

RegisterClientScriptBlock(Type, String, String, Boolean) RegisterClientScriptBlock(Type, String, String, Boolean) RegisterClientScriptBlock(Type, String, String, Boolean) RegisterClientScriptBlock(Type, String, String, Boolean)

使用型別、索引鍵、指令碼常值,以及表示是否加入指令碼標記的布林值,向 Page 物件註冊用戶端指令碼。Registers the client script with the Page object using a type, key, script literal, and Boolean value indicating whether to add script tags.

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) RegisterClientScriptInclude(Type, String, String)

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

RegisterClientScriptResource(Type, String) RegisterClientScriptResource(Type, String) RegisterClientScriptResource(Type, String) RegisterClientScriptResource(Type, String)

使用型別和資源名稱,向 Page 物件註冊用戶端指令碼資源。Registers the client script resource with the Page object using a type and a resource name.

RegisterExpandoAttribute(String, String, String) RegisterExpandoAttribute(String, String, String) RegisterExpandoAttribute(String, String, String) RegisterExpandoAttribute(String, String, String)

使用控制項 ID、屬性名稱和屬性值,將名稱/值組註冊為指定之控制項的自訂 (Expando) 屬性。Registers a name/value pair as a custom (expando) attribute of the specified control given a control ID, attribute name, and attribute value.

RegisterExpandoAttribute(String, String, String, Boolean) RegisterExpandoAttribute(String, String, String, Boolean) RegisterExpandoAttribute(String, String, String, Boolean) RegisterExpandoAttribute(String, String, String, Boolean)

使用控制項 ID、屬性名稱、屬性值,以及表示是否編碼屬性值的布林值,將名稱/值組註冊為指定之控制項的自訂 (Expando) 屬性。Registers a name/value pair as a custom (expando) attribute of the specified control given a control ID, an attribute name, an attribute value, and a Boolean value indicating whether to encode the attribute value.

RegisterForEventValidation(PostBackOptions) RegisterForEventValidation(PostBackOptions) RegisterForEventValidation(PostBackOptions) RegisterForEventValidation(PostBackOptions)

使用 PostBackOptions 註冊事件參考以進行驗證。Registers an event reference for validation with PostBackOptions.

RegisterForEventValidation(String) RegisterForEventValidation(String) RegisterForEventValidation(String) RegisterForEventValidation(String)

使用唯一的控制項 ID (表示產生事件的用戶端控制項) 註冊事件參考以進行驗證。Registers an event reference for validation with a unique control ID representing the client control generating the event.

RegisterForEventValidation(String, String) RegisterForEventValidation(String, String) RegisterForEventValidation(String, String) RegisterForEventValidation(String, String)

使用唯一的控制項 ID 和事件引數 (表示產生事件的用戶端控制項) 註冊事件參考以進行驗證。Registers an event reference for validation with a unique control ID and event arguments representing the client control generating the event.

RegisterHiddenField(String, String) RegisterHiddenField(String, String) RegisterHiddenField(String, String) RegisterHiddenField(String, String)

Page 物件註冊隱藏值。Registers a hidden value with the Page object.

RegisterOnSubmitStatement(Type, String, String) RegisterOnSubmitStatement(Type, String, String) RegisterOnSubmitStatement(Type, String, String) RegisterOnSubmitStatement(Type, String, String)

使用類型、索引鍵和指令碼常值 (Literal),向 Page 物件註冊 OnSubmit 陳述式。Registers an OnSubmit statement with the Page object using a type, a key, and a script literal. 當送出 HtmlForm 時,會執行該陳述式。The statement executes when the HtmlForm is submitted.

RegisterStartupScript(Type, String, String) RegisterStartupScript(Type, String, String) RegisterStartupScript(Type, String, String) RegisterStartupScript(Type, String, String)

使用型別、索引鍵和指令碼常值,向 Page 物件註冊啟始指令碼。Registers the startup script with the Page object using a type, a key, and a script literal.

RegisterStartupScript(Type, String, String, Boolean) RegisterStartupScript(Type, String, String, Boolean) RegisterStartupScript(Type, String, String, Boolean) RegisterStartupScript(Type, String, String, Boolean)

使用型別、索引鍵、指令碼常值,以及表示是否加入指令碼標記的布林值,向 Page 物件註冊啟始指令碼。Registers the startup script with the Page object using a type, a key, a script literal, and a Boolean value indicating whether to add script tags.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
ValidateEvent(String) ValidateEvent(String) ValidateEvent(String) ValidateEvent(String)

驗證使用 RegisterForEventValidation(String) 方法註冊以進行事件驗證的用戶端事件。Validates a client event that was registered for event validation using the RegisterForEventValidation(String) method.

ValidateEvent(String, String) ValidateEvent(String, String) ValidateEvent(String, String) ValidateEvent(String, String)

驗證使用 RegisterForEventValidation(String, String) 方法註冊以進行事件驗證的用戶端事件。Validates a client event that was registered for event validation using the RegisterForEventValidation(String, String) method.

適用於

另請參閱