Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System.Web.UI
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
ClientScriptManager Class

Note: This class is new in the .NET Framework version 2.0.

Defines methods for managing client-side scripts in Web applications.

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

Visual Basic (Declaration)
Public NotInheritable Class ClientScriptManager
Visual Basic (Usage)
Dim instance As ClientScriptManager
C#
public sealed class ClientScriptManager
C++
public ref class ClientScriptManager sealed
J#
public final class ClientScriptManager
JScript
public final class ClientScriptManager

The ClientScriptManager class is used to manage client-side 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-side script to a Web page declaratively by including the script in the HTML markup of the page. However, there are situations when adding client-side 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-side function that, when invoked, initiates a client call back to a server-side event. For more information, see Implementing Client Callbacks 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 require that ECMAScript is enabled on the client side. To check if the browser for a client 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-side 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-side script.

TopicLocation
Client Callback with Validation Implementation ExampleBuilding ASP .NET Web Applications
Client Callback with Validation Implementation ExampleBuilding ASP .NET Web Applications
Client Callback with Validation Implementation ExampleBuilding ASP .NET Web Applications in Visual Studio
Client-Callback Implementation (C#) ExampleBuilding ASP .NET Web Applications
Client-Callback Implementation (C#) ExampleBuilding ASP .NET Web Applications
Client-Callback Implementation (C#) ExampleBuilding ASP .NET Web Applications in Visual Studio
Client-Callback Implementation (Visual Basic) ExampleBuilding ASP .NET Web Applications
Client-Callback Implementation (Visual Basic) ExampleBuilding ASP .NET Web Applications
Client-Callback Implementation (Visual Basic) ExampleBuilding ASP .NET Web Applications in Visual Studio
How to: Implement Callbacks in ASP.NET Web PagesBuilding ASP .NET Web Applications
How to: Implement Callbacks in ASP.NET Web PagesBuilding ASP .NET Web Applications
How to: Implement Callbacks in ASP.NET Web PagesBuilding ASP .NET Web Applications in Visual Studio
Implementing Client Callbacks Programmatically Without Postbacks in ASP.NET Web PagesBuilding ASP .NET Web Applications in Visual Studio
Implementing Client Callbacks Without Postbacks in ASP.NET Web PagesBuilding ASP .NET Web Applications
Implementing Client Callbacks Without Postbacks in ASP.NET Web PagesBuilding ASP .NET Web Applications

The following code example demonstrates the use of the RegisterClientScriptBlock method of the ClientScriptManager class. Two client-side scripts are defined in the page: PopupScript, which displays a client-side alert message when the page is loaded, and ButtonClickScript, which defines a client-side handler for an HTML button's onClick event.

Visual Basic
<%@ Page Language="VB" %>

<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>
  <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>
C#
<%@ Page Language="C#"%>
<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>
  <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>
System.Object
  System.Web.UI.ClientScriptManager
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker