ScriptManager.RegisterClientScriptBlock Method (Control, Type, String, String, Boolean)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Registers a client script block with the ScriptManager control for use with a control that is inside an UpdatePanel control, and then adds the script block to the page.

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

Public Shared Sub RegisterClientScriptBlock (
	control As Control,
	type As Type,
	key As String,
	script As String,
	addScriptTags As Boolean


Type: System.Web.UI.Control

The control that is registering the client script block.

Type: System.Type

The type of the client script block. This parameter is usually specified by using the typeof operator (C#) or the GetType operator (Visual Basic) to retrieve the type of the control that is registering the script.

Type: System.String

A unique identifier for the script block.

Type: System.String

The script.

Type: System.Boolean

true to enclose the script block in <script> and </script> tags; otherwise, false.

Exception Condition

The client script block type is null.

- or -

The control that is registering the script block is null.


The control that is registering the script block is not in the page's control tree.

You use the RegisterClientScriptBlock method to register a client script block that is compatible with partial-page rendering and that has no Microsoft Ajax Library dependencies. Client script blocks that are registered by using this method are sent to the page only when control represents a control that is inside an UpdatePanel control that is being updated. To register a script block every time that an asynchronous postback occurs, use the RegisterClientScriptBlock(Page, Type, String, String, Boolean) overload of this method. 

If you want to register a script block that does not pertain to partial-page updates, and if you want to register the script block only one time during initial page rendering, use the RegisterClientScriptBlock method of the ClientScriptManager class. You can get a reference to the ClientScriptManager object from the ClientScript property of the page.

If addScriptTags is true, the RegisterClientScriptBlock method adds <script> tags around the script block. Pass false if you want to create <script> tags yourself, such as when you want to set the attributes of specific <script> tags. If addScriptTags is false and the script parameter contains multiple script blocks, an exception is thrown.

The RegisterClientScriptBlock method adds a script block to the page after the opening <form> tag. The script blocks are not guaranteed to be output in the same order in which they are registered. If the order of the script blocks is important, concatenate your script blocks into a single string (for example, by using the StringBuilder object), and then register them as a single client script block.

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<script runat="server">

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim script As String
        script = _
        "function ToggleItem(id)" & _
        "  {" & _
        "    var elem = $get('div'+id);" & _
        "    if (elem)" & _
        "    {" & _
        "      if ( != 'block') " & _
        "      {" & _
        " = 'block';" & _
        " = 'visible';" & _
        "      } " & _
        "      else" & _
        "      {" & _
        " = 'none';" & _
        " = 'hidden';" & _
        "      }" & _
        "    }" & _
        "  }"

        ScriptManager.RegisterClientScriptBlock( _
            Me, _
            GetType(Page), _
            "ToggleScript", _
            script, _

    End Sub

<html xmlns="">
<head id="Head1" runat="server">
    <title>ScriptManager RegisterClientScriptInclude</title>
    <form id="Form1" runat="server">
            <br />
            <asp:ScriptManager ID="ScriptManager1" 
            <asp:UpdatePanel ID="UpdatePanel1" 
                    <asp:XmlDataSource ID="XmlDataSource1"
                    <asp:DataList ID="DataList1" DataSourceID="XmlDataSource1"
                        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
                        BorderWidth="1px" CellPadding="3" GridLines="Horizontal"
                            <div style="font-size:larger; font-weight:bold; cursor:pointer;" 
                                 onclick='ToggleItem(<%# Eval("ID") %>);'>
                                <span><%# Eval("Name") %></span>
                            <div id='div<%# Eval("ID") %>' 
                                 style="display: block; visibility: visible;">
                                <span><%# Eval("Company") %></span>
                                <br />
                                <a href='<%# Eval("URL") %>' 
                                   title='<%# Eval("Name", "Link to the {0} Web site") %>'>
                                   <%# Eval("URL") %></a>
                                <hr />
                        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                        <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                        <AlternatingItemStyle BackColor="#F7F7F7" />
                        <ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
    <Contact id="1" 
             Name="Aaber, Jesper" 
             Company="A. Data Corporation" 
    <Contact id="2" 
             Name="Canel, Fabrice" 
             Company="Coho Winery" 
    <Contact id="3" 
             Name="Heloo, Waleed" 
             Company="Contoso, Ltd" 
    <Contact id="4" 
             Name="Rovik, Dag" 
             Company="Wingtip Toys" 

.NET Framework
Available since 3.5
Return to top