Documentation for: ASP.NET Ajax Version 1.0

This documentation is for a previous version. For the current released version, see the ASP.NET Ajax documentation on MSDN.

 

 

 

 

 

 

 

Skip Navigation Links.
ScriptManager Class
ScriptManager Constructor
AllowCustomErrorsRedirect Property
AsyncPostBackError Event
AsyncPostBackErrorMessage Property
AsyncPostBackSourceElementID Property
AsyncPostBackTimeout Property
AuthenticationService Property
EnablePageMethods Property
EnablePartialRendering Property
EnableScriptGlobalization Property
EnableScriptLocalization Property
GetCurrent Method (page)
IsDebuggingEnabled Property
IsInAsyncPostBack Property
LoadPostData Method (postDataKey, postCollection)
LoadScriptsBeforeUI Property
OnAsyncPostBackError Method (e)
OnInit Method (e)
OnPreRender Method (e)
OnResolveScriptReference Method (e)
ProfileService Property
RaisePostDataChangedEvent Method
RegisterArrayDeclaration Method
RegisterArrayDeclaration Method (control, arrayName, arrayValue)
RegisterArrayDeclaration Method (page, arrayName, arrayValue)
RegisterAsyncPostBackControl Method (control)
RegisterClientScriptBlock Method
RegisterClientScriptBlock Method (control, type, key, script, addScriptTags)
RegisterClientScriptBlock Method (page, type, key, script, addScriptTags)
RegisterClientScriptInclude Method
RegisterClientScriptInclude Method (control, type, key, url)
RegisterClientScriptInclude Method (page, type, key, url)
RegisterClientScriptResource Method
RegisterClientScriptResource Method (control, type, resourceName)
RegisterClientScriptResource Method (page, type, resourceName)
RegisterDataItem Method
RegisterDataItem Method (control, dataItem)
RegisterDataItem Method (control, dataItem, isJsonSerialized)
RegisterDispose Method (control, disposeScript)
RegisterExpandoAttribute Method (control, controlId, attributeName, attributeValue, encode)
RegisterExtenderControl Method (extenderControl, targetControl)
RegisterHiddenField Method
RegisterHiddenField Method (control, hiddenFieldName, hiddenFieldInitialValue)
RegisterHiddenField Method (page, hiddenFieldName, hiddenFieldInitialValue)
RegisterOnSubmitStatement Method
RegisterOnSubmitStatement Method (control, type, key, script)
RegisterOnSubmitStatement Method (page, type, key, script)
RegisterPostBackControl Method (control)
RegisterScriptControl Method (scriptControl)
RegisterScriptDescriptors Method
RegisterScriptDescriptors Method (extenderControl)
RegisterScriptDescriptors Method (scriptControl)
RegisterStartupScript Method
RegisterStartupScript Method (control, type, key, script, addScriptTags)
RegisterStartupScript Method (page, type, key, script, addScriptTags)
Render Method (writer)
ResolveScriptReference Event
ScriptMode Property
ScriptPath Property
Scripts Property
Services Property
SetFocus Method
SetFocus Method (clientID)
SetFocus Method (control)
SupportsPartialRendering Property
Visible Property

RegisterClientScriptBlock Method (control, type, key, script, addScriptTags)

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

Syntax

CSharp

public static void RegisterClientScriptBlock (
	Control control,
	Type type,
	string key,
	string script,
	bool addScriptTags
)

VisualBasic

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

ManagedCPlusPlus

public:
static void RegisterClientScriptBlock (
	Control^ control, 
	Type^ type, 
	String^ key, 
	String^ script, 
	bool addScriptTags
)

JSharp

public static void RegisterClientScriptBlock (
	Control control,
	Type type,
	String key,
	String script,
	boolean addScriptTags
)

Parameters

control (Control)

The control that is registering the client script block.

type (Type)

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

key (String)

The string that uniquely identifies the script block.

script (String)

A string that contains the script.

addScriptTags (Boolean)

A Boolean value that indicates whether to enclose the script block in <script> tags.

Remarks

The RegisterClientScriptBlock method is used 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 with this method are sent to the page only when the control parameter is inside an UpdatePanel control that is updated. To register a script file each time an asynchronous postback occurs, use the RegisterClientScriptBlock(Page, Type, String, String, Boolean) overload of this method. If you are registering a script file that does not pertain to partial-page updates and you want to register a script file only one time during initial page rendering, use the RegisterClientScriptBlock(Type, String, String) method of the ClientScriptManager class. You can get a reference to the ClientScriptManager object from the ClientScript property of the page.

If the addScriptTags parameter is true, the RegisterClientScriptBlock method adds <script> tags around the script literal. Pass false for addScriptTags when 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> element 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.

Examples

CS

<%@ 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">
    protected void Page_PreRender(object sender, EventArgs e)
    {
        string script = @"
        function ToggleItem(id)
          {
            var elem = $get('div'+id);
            if (elem) 
            {
              if (elem.style.display != 'block') 
              {
                elem.style.display = 'block';
                elem.style.visibility = 'visible';
              } 
              else
              {
                elem.style.display = 'none';
                elem.style.visibility = 'hidden';
              }
            }
          }
        ";

        ScriptManager.RegisterClientScriptBlock(
            this,
            typeof(Page),
            "ToggleScript",
            script,
            true);
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ScriptManager RegisterClientScriptInclude</title>
</head>
<body>
    <form id="Form1" runat="server">
        <div>
            <br />
            <asp:ScriptManager ID="ScriptManager1" 
                                 EnablePartialRendering="true"
                                 runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" 
                               UpdateMode="Conditional"
                               runat="server">
                <ContentTemplate>
                    <asp:XmlDataSource ID="XmlDataSource1"
                                       DataFile="~/App_Data/Contacts.xml"
                                       XPath="Contacts/Contact"
                                       runat="server"/>
                    <asp:DataList ID="DataList1" DataSourceID="XmlDataSource1"
                        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
                        BorderWidth="1px" CellPadding="3" GridLines="Horizontal"
                        runat="server">
                        <ItemTemplate>
                            <div style="font-size:larger; font-weight:bold; cursor:pointer;" 
                                 onclick='ToggleItem(<%# Eval("ID") %>);'>
                                <span><%# Eval("Name") %></span>
                            </div>
                            <div id='div<%# Eval("ID") %>' 
                                 style="display: block; visibility: visible;">
                                <span><%# Eval("Company") %></span>
                                <br />
                                <a href='<%# Eval("URL") %>' 
                                   target="_blank" 
                                   title='<%# Eval("Name", "Link to the {0} Web site") %>'>
                                   <%# Eval("URL") %></a>
                                </asp:LinkButton>
                                <hr />
                            </div>
                        </ItemTemplate>
                        <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" />
                    </asp:DataList>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>

vb

<%@ 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_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 (elem.style.display != 'block') " & _
        "      {" & _
        "        elem.style.display = 'block';" & _
        "        elem.style.visibility = 'visible';" & _
        "      } " & _
        "      else" & _
        "      {" & _
        "        elem.style.display = 'none';" & _
        "        elem.style.visibility = 'hidden';" & _
        "      }" & _
        "    }" & _
        "  }"

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

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ScriptManager RegisterClientScriptInclude</title>
</head>
<body>
    <form id="Form1" runat="server">
        <div>
            <br />
            <asp:ScriptManager ID="ScriptManager1" 
                                 EnablePartialRendering="true"
                                 runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" 
                               UpdateMode="Conditional"
                               runat="server">
                <ContentTemplate>
                    <asp:XmlDataSource ID="XmlDataSource1"
                                       DataFile="~/App_Data/Contacts.xml"
                                       XPath="Contacts/Contact"
                                       runat="server"/>
                    <asp:DataList ID="DataList1" DataSourceID="XmlDataSource1"
                        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
                        BorderWidth="1px" CellPadding="3" GridLines="Horizontal"
                        runat="server">
                        <ItemTemplate>
                            <div style="font-size:larger; font-weight:bold; cursor:pointer;" 
                                 onclick='ToggleItem(<%# Eval("ID") %>);'>
                                <span><%# Eval("Name") %></span>
                            </div>
                            <div id='div<%# Eval("ID") %>' 
                                 style="display: block; visibility: visible;">
                                <span><%# Eval("Company") %></span>
                                <br />
                                <a href='<%# Eval("URL") %>' 
                                   target="_blank" 
                                   title='<%# Eval("Name", "Link to the {0} Web site") %>'>
                                   <%# Eval("URL") %></a>
                                </asp:LinkButton>
                                <hr />
                            </div>
                        </ItemTemplate>
                        <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" />
                    </asp:DataList>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>

App_Data

<Contacts>
    <Contact id="1" 
             Name="Aaber, Jesper" 
             Company="A. Data Corporation" 
             URL="http://www.adatum.com/"/>
    <Contact id="2" 
             Name="Canel, Fabrice" 
             Company="Coho Winery" 
             URL="http://www.cohowinery.com/"/>
    <Contact id="3" 
             Name="Heloo, Waleed" 
             Company="Contoso, Ltd" 
             URL="http://www.contoso.com/"/>
    <Contact id="4" 
             Name="Rovik, Dag" 
             Company="Wingtip Toys" 
             URL="http://www.wingtiptoys.com/"/>
</Contacts>

Exceptions

ExceptionCondition
ArgumentNullException

The client script block type is null.

- or -

The control that is registering the script block is null.

ArgumentException

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

Assembly: System.Web.Extensions (Module: System.Web.Extensions)