Bewerken

Share via


ClientScriptManager.RegisterClientScriptInclude Method

Definition

Registers the client script include with the Page object.

Overloads

RegisterClientScriptInclude(String, String)

Registers the client script with the Page object using a key and a URL, which enables the script to be called from the client.

RegisterClientScriptInclude(Type, String, String)

Registers the client script include with the Page object using a type, a key, and a URL.

RegisterClientScriptInclude(String, String)

Registers the client script with the Page object using a key and a URL, which enables the script to be called from the client.

public:
 void RegisterClientScriptInclude(System::String ^ key, System::String ^ url);
public void RegisterClientScriptInclude (string key, string url);
member this.RegisterClientScriptInclude : string * string -> unit
Public Sub RegisterClientScriptInclude (key As String, url As String)

Parameters

key
String

The key of the client script include to register.

url
String

The URL of the client script include to register.

Examples

For related information, including syntax, usage, and an example, see RegisterClientScriptInclude.

Remarks

A client script include is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page. Attempting to register a script that is already registered does not create a duplicate of the script.

Call the IsClientScriptIncludeRegistered method to determine whether a client script include with a given key and type pair is already registered and avoid unnecessarily attempting to add the script.

Note

To resolve the client URL, use the ResolveClientUrl method. This method uses the context of the URL on which it is called to resolve the path.

This overload of the RegisterClientScriptInclude method calls the overload that takes a key, a URL, and a type parameter.

The method adds a script block at the top of the rendered page.

See also

Applies to

RegisterClientScriptInclude(Type, String, String)

Registers the client script include with the Page object using a type, a key, and a URL.

public:
 void RegisterClientScriptInclude(Type ^ type, System::String ^ key, System::String ^ url);
public void RegisterClientScriptInclude (Type type, string key, string url);
member this.RegisterClientScriptInclude : Type * string * string -> unit
Public Sub RegisterClientScriptInclude (type As Type, key As String, url As String)

Parameters

type
Type

The type of the client script include to register.

key
String

The key of the client script include to register.

url
String

The URL of the client script include to register.

Exceptions

The client script include type is null.

The URL is null.

-or-

The URL is empty.

Examples

The following code example demonstrates the use of the RegisterClientScriptInclude method. Note that if the logic to check for the existing client script include were removed, there would still not be duplicate client scripts in the rendered page because the RegisterClientScriptInclude method checks for duplicates. The benefit of checking is to reduce unnecessary computation.

<%@ 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)
    {
        // Define the name, type and url of the client script on the page.
        String csname = "ButtonClickScript";
        String csurl = "~/script_include.js";
        Type cstype = this.GetType();

        // Get a ClientScriptManager reference from the Page class.
        ClientScriptManager cs = Page.ClientScript;

        // Check to see if the include script exists already.
        if (!cs.IsClientScriptIncludeRegistered(cstype, csname))
        {
            cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl));
        }

    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form id="Form1" runat="server">
     <div>
        <input type="text"
               id="Message"/> 
        <input type="button" 
               value="ClickMe"
               onclick="DoClick()"/>
     </div>
     </form>
  </body>
</html>
<%@ 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_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        ' Define the name, type and url of the client script on the page.
        Dim csname As String = "ButtonClickScript"
        Dim csurl As String = "~/script_include.js"
        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 include script is already registered.
        If (Not cs.IsClientScriptIncludeRegistered(cstype, csname)) Then
      
            cs.RegisterClientScriptInclude(cstype, csname, ResolveClientUrl(csurl))
      
        End If
    
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ClientScriptManager Example</title>
</head>
<body>
     <form id="Form1" runat="server">
     <div>
        <input type="text"
               id="Message"/> 
        <input type="button" 
               value="ClickMe"
               onclick="DoClick()"/>
     </div>
     </form>
</body>
</html>

This example requires a JavaScript file named Script_include.js with the following contents:

function DoClick() {Form1.Message.value='Text from include script.'}  

Remarks

This overload of the RegisterClientScriptInclude method takes key and url parameters to identify the script, as well as a type parameter to specify the identification of the client script include. You specify the type based on the object that will be accessing the resource. For instance, when using a Page instance to access the resource, you specify the Page type.

Note

To resolve the client URL, use the ResolveClientUrl method. This method uses the context of the URL on which it is called to resolve the path.

This method adds a script block at the top of the rendered page.

See also

Applies to