ClientScriptManager 类

定义

在 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
继承
ClientScriptManager

示例

下面的代码示例演示如何使用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 Page对象的ClientScript属性中获取对类的引用。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 网页。For more information, see How to: Add Client Script Dynamically to ASP.NET Web Pages.

类通过键StringType来唯一标识脚本。 ClientScriptManagerThe 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.

如果需要在不执行回发的情况下从客户端运行服务器代码, 则可以使用类来调用客户端回调。ClientScriptManagerThe 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 网页。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使用属性, 该属性可通过Browser ASP.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如果使用Button控件UseSubmitBehavior并且属性设置为false, 则可以使用GetPostBackEventReference方法返回控件的客户端回发事件。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.

Button控件、 控件和LinkButton控件的属性可用于运行客户端脚本。 OnClientClick ImageButtonThe OnClientClick property of the Button control, ImageButton control, and LinkButton control can be used to run client script.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
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)

获取一个对客户端函数的引用;调用该函数时,将启动一个对服务器事件的客户端回调。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)

获取一个对客户端函数的引用;调用该函数时,将启动一个对服务器事件的客户端回调。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)

获取一个对客户端函数的引用;调用该函数时,将启动一个对服务器事件的客户端回调。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()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
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)

获取一个引用,并在其开头附加 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)

返回一个可以在客户端事件中使用的字符串,以便回发到服务器。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)

返回一个在客户端事件中使用的字符串,以便回发到服务器。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)

返回一个可以在客户端事件中使用的字符串,以便回发到服务器。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)

返回一个可以在客户端事件中使用的字符串,以便回发到服务器。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()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
GetWebResourceUrl(Type, String)

获取对程序集内资源的 URL 引用。Gets a URL reference to a resource in an assembly.

IsClientScriptBlockRegistered(String)

使用指定键确定 Page 对象是否注册了客户端脚本块。Determines whether the client script block is registered with the Page object using the specified key.

IsClientScriptBlockRegistered(Type, String)

使用键和类型确定 Page 对象是否注册了客户端脚本块。Determines whether the client script block is registered with the Page object using a key and type.

IsClientScriptIncludeRegistered(String)

使用指定键确定 Page 对象是否注册了客户端脚本包含。Determines whether the client script include is registered with the Page object using the specified key.

IsClientScriptIncludeRegistered(Type, String)

使用键和类型确定 Page 对象是否注册了客户端脚本包含。Determines whether the client script include is registered with the Page object using a key and type.

IsOnSubmitStatementRegistered(String)

确定 Page 对象是否使用指定键注册了 OnSubmit 语句。Determines whether the OnSubmit statement is registered with the Page object using the specified key.

IsOnSubmitStatementRegistered(Type, String)

确定 Page 对象是否使用指定的键和类型注册了 OnSubmit 语句。Determines whether the OnSubmit statement is registered with the Page object using the specified key and type.

IsStartupScriptRegistered(String)

使用指定键确定 Page 对象是否注册了启动脚本。Determines whether the startup script is registered with the Page object using the specified key.

IsStartupScriptRegistered(Type, String)

使用指定键和类型确定 Page 对象是否注册了启动脚本。Determines whether the startup script is registered with the Page object using the specified key and type.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
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)

使用类型、键和脚本文本向 Page 对象注册客户端脚本。Registers the client script with the Page object using a type, key, and script literal.

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)

使用让客户端能够调用脚本的键和 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)

使用类型、键和 URL 向 Page 对象注册客户端脚本包含。Registers the client script include with the Page object using a type, a key, and a URL.

RegisterClientScriptResource(Type, String)

使用类型和资源名向 Page 对象注册客户端脚本资源。Registers the client script resource with the Page object using a type and a resource name.

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)

在给定了控件 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)

通过 PostBackOptions 为验证注册事件引用。Registers an event reference for validation with PostBackOptions.

RegisterForEventValidation(String)

使用唯一控件 ID(表示生成事件的客户端控件)注册事件引用以进行验证。Registers an event reference for validation with a unique control ID representing the client control generating the event.

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)

Page 对象注册一个隐藏值。Registers a hidden value with the Page object.

RegisterOnSubmitStatement(Type, String, String)

使用类型、键和脚本文本向 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)

使用类型、键和脚本文本向 Page 对象注册启动脚本。Registers the startup script with the Page object using a type, a key, and a script literal.

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()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
ValidateEvent(String)

验证使用 RegisterForEventValidation(String) 方法为事件验证注册的客户端事件。Validates a client event that was registered for event validation using the RegisterForEventValidation(String) method.

ValidateEvent(String, String)

验证使用 RegisterForEventValidation(String, String) 方法为事件验证注册的客户端事件。Validates a client event that was registered for event validation using the RegisterForEventValidation(String, String) method.

适用于

另请参阅