ClientScriptManager.RegisterClientScriptResource(Type, String) Methode

Definition

Registriert die Clientskriptressource unter Verwendung eines Typ und eines Ressourcennamens für das Page-Objekt.Registers the client script resource with the Page object using a type and a resource name.

public:
 void RegisterClientScriptResource(Type ^ type, System::String ^ resourceName);
public void RegisterClientScriptResource (Type type, string resourceName);
member this.RegisterClientScriptResource : Type * string -> unit

Parameter

type
Type

Der Typ der zu registrierenden Clientskriptressource.The type of the client script resource to register.

resourceName
String

Der Name der zu registrierenden Clientskriptressource.The name of the client script resource to register.

Ausnahmen

Der Clientressourcentyp ist null.The client resource type is null.

- oder --or-

Der Clientressourcenname ist null.The client resource name is null.

- oder --or- Der Clientressourcenname hat eine Länge von 0 (null).The client resource name has a length of zero.

Beispiele

Im folgenden Codebeispiel wird die Verwendung RegisterClientScriptResource der-Methode veranschaulicht.The following code example demonstrates the use of the RegisterClientScriptResource method.

<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
  <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>
<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
  <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>

Im folgenden Codebeispiel wird veranschaulicht, wie das WebResourceAttribute Metadatenattribut Programm gesteuert angewendet wird, um die Assembly für die zu verwendenden Ressourcen zu markieren.The following code example demonstrates how to programmatically apply the WebResourceAttribute metadata attribute to mark the assembly for the resources that will be served.

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.

    }
     
}
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

Für dieses Beispiel ist eine JavaScript- Script_include.jsDatei mit dem Namen mit folgendem Inhalt erforderlich:This example requires a JavaScript file named Script_include.js, with the following contents:

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

Kompilieren Sie Script_include.js die Datei als Ressource in der Samples.AspNet.CS.Controls Assembly, die die ClientScriptResourceLabel -Klasse enthält.Compile the Script_include.js file as a resource in the Samples.AspNet.CS.Controls assembly that contains the ClientScriptResourceLabel class.

Hinweise

Die RegisterClientScriptResource -Methode wird beim Zugriff auf kompilierte Ressourcen aus Assemblys über den WebResource. axd-HTTP-Handler verwendet.The RegisterClientScriptResource method is used when accessing compiled-in resources from assemblies through the WebResource.axd HTTP handler. Die RegisterClientScriptResource -Methode registriert das Skript mit Page dem-Objekt und verhindert doppelte Skripts.The RegisterClientScriptResource method registers the script with the Page object and prevents duplicate scripts. Diese Methode umschließt den Inhalt der Ressourcen-URL mit <script> einem-Element Block.This method wraps the contents of the resource URL with a <script> element block.

Gilt für:

Siehe auch