Export (0) Print
Expand All

ScriptManager.RegisterClientScriptInclude Method (Control, Type, String, String)

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

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

static member RegisterClientScriptInclude : 
        control:Control * 
        type:Type * 
        key:string * 
        url:string -> unit

Parameters

control
Type: System.Web.UI.Control

The control that is registering the client script file.

type
Type: System.Type

The type of the client script file. 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.

key
Type: System.String

A unique identifier for the script file.

url
Type: System.String

The URL of the script file.

ExceptionCondition
ArgumentNullException

The client script file type is a null reference (Nothing in Visual Basic).

- or -

The control that is registering the script file is a null reference (Nothing in Visual Basic).

ArgumentException

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

- or -

url is a null reference (Nothing in Visual Basic).

- or -

url is empty.

You use the RegisterClientScriptInclude method to register a client script file for a page or part of a page that is participating in partial-page updates. Client script files that are registered by using this method are sent to the page only when the control represents a control that is inside an UpdatePanel control that is being updated. To register a script file every time that an asynchronous postback occurs, use the RegisterClientScriptInclude(Page, Type, String, String) 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.

The RegisterClientScriptInclude method registers a client script file on the page by rendering a script element whose opening tag contains a src attribute. The url parameter is used to set the src attribute. To resolve URLs, use the ResolveClientUrl method. This method uses the context of the URL it is called for to resolve the path.

Both the RegisterClientScriptInclude and RegisterClientScriptResource methods render script files to the browser. If a script with the same type and key (for a script file) or the same type and resource name (for an embedded resource) is already rendered, the script is not rendered again.

<%@ 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)
    {
        if (!IsPostBack)
            Calendar1.SelectedDate = DateTime.Today;

    }
    protected void Page_PreRender(object sender, EventArgs e)
    {
        ScriptManager.RegisterClientScriptInclude(
            this,
            typeof(Page),
            "AlertScript",
            ResolveClientUrl("~/scripts/script_alertdiv.js"));
    }
    protected void IncrementButton_Click(object sender, EventArgs e)
    {
        Calendar1.SelectedDate = Calendar1.SelectedDate.AddDays(1.0);
    }
    protected void DecrementButton_Click(object sender, EventArgs e)
    {
        Calendar1.SelectedDate = Calendar1.SelectedDate.AddDays(-1.0);
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ScriptManager RegisterClientScriptInclude</title>
    <style type="text/css">
	div.MessageStyle
	{
      background-color: Green;
      top: 95%;
      left: 1%;
      position: absolute;
      visibility: hidden;
    }
	</style>
</head>
<body>
    <form id="Form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1"
                               runat="server"/>

            <script type="text/javascript">
            Sys.WebForms.PageRequestManager.instance.add_endRequest(Notify);
            </script>

            <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional"
                runat="server">
                <ContentTemplate>
                    <asp:Calendar ID="Calendar1" runat="server"/>
                    <br />
                    Change the selected date: 
                    <asp:Button runat="server" ID="DecrementButton" Text="-" OnClick="DecrementButton_Click" />
                    <asp:Button runat="server" ID="IncrementButton" Text="+" OnClick="IncrementButton_Click" />
                </ContentTemplate>
            </asp:UpdatePanel>

            <div id="NotifyDiv" class="MessageStyle">
                Updates are complete.
            </div>
        </div>
    </form>
</body>
</html>
function Notify(sender, arg)
{
    ActivateAlertDiv('visible', 'NotifyDiv');
    setTimeout("ActivateAlertDiv('hidden', 'NotifyDiv')", 1000);
}
function ActivateAlertDiv(visstring, elem)
{
    var adiv = document.getElementById(elem);
    adiv.style.visibility = visstring;
}

.NET Framework

Supported in: 4.5, 4, 3.5

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.

Show:
© 2014 Microsoft