Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 RegisterClientScriptResource Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ClientScriptManager..::.RegisterClientScriptResource Method

Registers the client script resource with the Page object using a type and a resource name.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Visual Basic (Declaration)
Public Sub RegisterClientScriptResource ( _
    type As Type, _
    resourceName As String _
)
Visual Basic (Usage)
Dim instance As ClientScriptManager
Dim type As Type
Dim resourceName As String

instance.RegisterClientScriptResource(type, _
    resourceName)
C#
public void RegisterClientScriptResource(
    Type type,
    string resourceName
)
Visual C++
public:
void RegisterClientScriptResource(
    Type^ type, 
    String^ resourceName
)
JScript
public function RegisterClientScriptResource(
    type : Type, 
    resourceName : String
)

Parameters

type
Type: System..::.Type
The type of the client script resource to register.
resourceName
Type: System..::.String
The name of the client script resource to register.
ExceptionCondition
ArgumentNullException

The client resource type is nullNothingnullptra null reference (Nothing in Visual Basic).

ArgumentNullException

The client resource name is nullNothingnullptra null reference (Nothing in Visual Basic).

- or -

The client resource name has a length of zero.

The RegisterClientScriptResource method is used when accessing compiled-in resources from assemblies through the WebResource.axd HTTP handler. The RegisterClientScriptResource method registers the script with the Page object and prevents duplicate scripts. This method wraps the contents of the resource URL with a <script> element block.

The following code example demonstrates the use of the RegisterClientScriptResource method.

Visual Basic
<%@ Page Language="VB" %>
<%@ Import Namespace="Samples.AspNet.VB.Controls" %>

<!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 resource name and type.
    Dim rsname As String = "Samples.AspNet.VB.Controls.script_include.js"
    Dim rstype As Type = GetType(ClientScriptResourceLabel)

    ' Get a ClientScriptManager reference from the Page class.
    Dim cs As ClientScriptManager = Page.ClientScript

    ' Write out the web resource url.
    ResourcePath.InnerHtml = cs.GetWebResourceUrl(rstype, rsname)

    ' Register the client resource with the page.
    cs.RegisterClientScriptResource(rstype, rsname)

  End Sub

</script>

<html  >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form    id="Form1"
            runat="server">
     The web resource path is 
     <span  id="ResourcePath"
            runat="server"/>.
     <br />
     <br />
     <input type="text" 
            id="Message" />     
     <input type="button" 
            onclick="DoClick()" 
            value="ClientClick" />
     </form>
  </body>
</html>
C#
<%@ Page Language="C#"%>
<%@ Import Namespace="Samples.AspNet.CS.Controls" %>

<!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 resource name and type.
    String rsname = "Samples.AspNet.CS.Controls.script_include.js";
    Type rstype = typeof(ClientScriptResourceLabel);

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

    // Write out the web resource url.
    ResourcePath.InnerHtml = cs.GetWebResourceUrl(rstype, rsname);

    // Register the client resource with the page.
    cs.RegisterClientScriptResource(rstype, rsname);

  }
</script>
<html  >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form    id="Form1"
            runat="server">
     The web resource path is 
     <span  id="ResourcePath"
            runat="server"/>.
     <br />
     <br />
     <input type="text" 
            id="Message" />     
     <input type="button" 
            onclick="DoClick()" 
            value="ClientClick" />
     </form>
  </body>
</html>

The following code example demonstrates how to programmatically apply the WebResourceAttribute metadata attribute to mark the assembly for the resources that will be served.

Visual Basic
Imports Microsoft.VisualBasic
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Security.Permissions

<Assembly: WebResource("Samples.AspNet.VB.Controls.script_include.js", "application/x-javascript")> 
Namespace Samples.AspNet.VB.Controls

    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public Class ClientScriptResourceLabel

        ' Class code goes here.

    End Class

End Namespace
C#
using System;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;

[assembly: WebResource("Samples.AspNet.CS.Controls.script_include.js", "application/x-javascript")]
namespace Samples.AspNet.CS.Controls
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class ClientScriptResourceLabel
    {
        // Class code goes here.

    }
     
}

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

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

Compile the Script_include.js file as a resource in the Samples.AspNet.CS.Controls assembly that contains the ClientScriptResourceLabel class.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker