Export (0) Print
Expand All

ClientScriptManager Class

Defines methods for managing client scripts in Web applications.

System.Object
  System.Web.UI.ClientScriptManager

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public sealed class ClientScriptManager

The ClientScriptManager type exposes the following members.

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetCallbackEventReference(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.
Public methodGetCallbackEventReference(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.
Public methodGetCallbackEventReference(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.
Public methodGetCallbackEventReference(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.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetPostBackClientHyperlink(Control, String)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.
Public methodGetPostBackClientHyperlink(Control, String, Boolean)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.
Public methodGetPostBackEventReference(PostBackOptions)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 PostBackOptions instance.
Public methodGetPostBackEventReference(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.
Public methodGetPostBackEventReference(PostBackOptions, Boolean)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 PostBackOptions object. Optionally, registers the event reference for validation.
Public methodGetPostBackEventReference(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.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetWebResourceUrlGets a URL reference to a resource in an assembly.
Public methodIsClientScriptBlockRegistered(String)Determines whether the client script block is registered with the Page object using the specified key.
Public methodIsClientScriptBlockRegistered(Type, String)Determines whether the client script block is registered with the Page object using a key and type.
Public methodIsClientScriptIncludeRegistered(String)Determines whether the client script include is registered with the Page object using the specified key.
Public methodIsClientScriptIncludeRegistered(Type, String)Determines whether the client script include is registered with the Page object using a key and type.
Public methodIsOnSubmitStatementRegistered(String)Determines whether the OnSubmit statement is registered with the Page object using the specified key.
Public methodIsOnSubmitStatementRegistered(Type, String)Determines whether the OnSubmit statement is registered with the Page object using the specified key and type.
Public methodIsStartupScriptRegistered(String)Determines whether the startup script is registered with the Page object using the specified key.
Public methodIsStartupScriptRegistered(Type, String)Determines whether the startup script is registered with the Page object using the specified key and type.
Public methodRegisterArrayDeclarationRegisters a JavaScript array declaration with the Page object using an array name and array value.
Public methodRegisterClientScriptBlock(Type, String, String)Registers the client script with the Page object using a type, key, and script literal.
Public methodRegisterClientScriptBlock(Type, String, String, Boolean)Registers the client script with the Page object using a type, key, script literal, and Boolean value indicating whether to add script tags.
Public methodRegisterClientScriptInclude(String, String)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 methodRegisterClientScriptInclude(Type, String, String)Registers the client script include with the Page object using a type, a key, and a URL.
Public methodRegisterClientScriptResourceRegisters the client script resource with the Page object using a type and a resource name.
Public methodRegisterExpandoAttribute(String, String, String)Registers a name/value pair as a custom (expando) attribute of the specified control given a control ID, attribute name, and attribute value.
Public methodRegisterExpandoAttribute(String, String, String, Boolean)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.
Public methodRegisterForEventValidation(PostBackOptions)Registers an event reference for validation with PostBackOptions.
Public methodRegisterForEventValidation(String)Registers an event reference for validation with a unique control ID representing the client control generating the event.
Public methodRegisterForEventValidation(String, String)Registers an event reference for validation with a unique control ID and event arguments representing the client control generating the event.
Public methodRegisterHiddenFieldRegisters a hidden value with the Page object.
Public methodRegisterOnSubmitStatementRegisters an OnSubmit statement with the Page object using a type, a key, and a script literal. The statement executes when the HtmlForm is submitted.
Public methodRegisterStartupScript(Type, String, String)Registers the startup script with the Page object using a type, a key, and a script literal.
Public methodRegisterStartupScript(Type, String, String, Boolean)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.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodValidateEvent(String)Validates a client event that was registered for event validation using the RegisterForEventValidation method.
Public methodValidateEvent(String, String)Validates a client event that was registered for event validation using the RegisterForEventValidation method.
Top

The ClientScriptManager class is used to manage client scripts and add them to Web applications. You can get a reference to the ClientScriptManager class from the ClientScript property of the Page object.

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. 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. For more information, see How to: Add Client Script Dynamically to ASP.NET Web Pages.

The 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.

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. 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. 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 Programmatically Without Postbacks in ASP.NET Web Pages.

NoteNote

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. 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.

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.

NoteNote

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.

The OnClientClick property of the Button control, ImageButton control, and LinkButton control can be used to run client script.

The following code example demonstrates the use of the RegisterClientScriptBlock method of the ClientScriptManager class. 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>

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft