ClientScriptManager.RegisterClientScriptResource(Type, String) ClientScriptManager.RegisterClientScriptResource(Type, String) ClientScriptManager.RegisterClientScriptResource(Type, String) Method

定義

使用型別和資源名稱,向 Page 物件註冊用戶端指令碼資源。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

參數

type
Type Type Type

要註冊的用戶端指令碼資源類別。The type of the client script resource to register.

resourceName
String String String

要註冊的用戶端指令碼資源名稱。The name of the client script resource to register.

例外狀況

用戶端資源類型為 nullThe client resource type is null.

-或--or-

用戶端資源名稱為 nullThe client resource name is null.

-或--or- 用戶端資源名稱長度為零。The client resource name has a length of zero.

範例

下列程式碼範例示範如何使用RegisterClientScriptResource方法。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>

下列程式碼範例示範如何以程式設計方式WebResourceAttribute套用中繼資料屬性, 以標記將提供服務的資源元件。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

這個範例需要名為Script_include.js的 JavaScript 檔案, 其中包含下列內容:This example requires a JavaScript file named Script_include.js, with the following contents:

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

將檔案編譯為包含ClientScriptResourceLabel類別之Samples.AspNet.CS.Controls元件中的資源。 Script_include.jsCompile the Script_include.js file as a resource in the Samples.AspNet.CS.Controls assembly that contains the ClientScriptResourceLabel class.

備註

RegisterClientScriptResource元件透過 WebResource HTTP 處理常式存取編譯的資源時, 會使用方法。The RegisterClientScriptResource method is used when accessing compiled-in resources from assemblies through the WebResource.axd HTTP handler. RegisterClientScriptResource方法會Page向物件註冊腳本, 並防止重複的腳本。The RegisterClientScriptResource method registers the script with the Page object and prevents duplicate scripts. 這個方法會將資源 URL <script>的內容包裝在元素區塊中。This method wraps the contents of the resource URL with a <script> element block.

適用於

另請參閱