Szerkesztés

Megosztás a következőn keresztül:


ScriptManager.RegisterClientScriptInclude Method

Definition

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.

Overloads

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

RegisterClientScriptInclude(Page, Type, String, String)

Registers client script with the ScriptManager control every time that an asynchronous postback occurs, and then adds a script file reference to the page.

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

public:
 static void RegisterClientScriptInclude(System::Web::UI::Control ^ control, Type ^ type, System::String ^ key, System::String ^ url);
public static void RegisterClientScriptInclude (System.Web.UI.Control control, Type type, string key, string url);
static member RegisterClientScriptInclude : System.Web.UI.Control * Type * string * string -> unit
Public Shared Sub RegisterClientScriptInclude (control As Control, type As Type, key As String, url As String)

Parameters

control
Control

The control that is registering the client script file.

type
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
String

A unique identifier for the script file.

url
String

The URL of the script file.

Exceptions

The client script file type is null.

-or-

The control that is registering the script file is null.

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

-or-

url is null.

-or-

url is empty.

Examples

<%@ 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;
}

Remarks

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.

See also

Applies to

RegisterClientScriptInclude(Page, Type, String, String)

Registers client script with the ScriptManager control every time that an asynchronous postback occurs, and then adds a script file reference to the page.

public:
 static void RegisterClientScriptInclude(System::Web::UI::Page ^ page, Type ^ type, System::String ^ key, System::String ^ url);
public static void RegisterClientScriptInclude (System.Web.UI.Page page, Type type, string key, string url);
static member RegisterClientScriptInclude : System.Web.UI.Page * Type * string * string -> unit
Public Shared Sub RegisterClientScriptInclude (page As Page, type As Type, key As String, url As String)

Parameters

page
Page

The page object that is registering the client script file.

type
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
String

A unique identifier for the script file.

url
String

The URL of the script file.

Exceptions

The client script file type is null.

-or-

The page that is registering the script file is null.

url is null.

-or-

url is empty.

Remarks

When you register a script file with this method, the script is rendered every time that an asynchronous postback occurs. To register a script file for a control that is inside an UpdatePanel control so that script is registered only when the UpdatePanel control is updated, use the RegisterClientScriptInclude(Control, 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.

See also

Applies to