ClientScriptManager.RegisterClientScriptResource メソッド
アセンブリ: System.Web (system.web.dll 内)
構文
Dim instance As ClientScriptManager Dim type As Type Dim resourceName As String instance.RegisterClientScriptResource(type, resourceName)
RegisterClientScriptResource メソッドは、WebResource.axd HTTP ハンドラ経由でアセンブリからコンパイル済みリソースにアクセスする際に使用されます。RegisterClientScriptResource メソッドは、スクリプトを Page オブジェクトに登録し、スクリプトの重複を防ぎます。このメソッドは、リソース URL の内容を <script> 要素ブロックでラップします。
RegisterClientScriptResource メソッドの使用方法を示すコード例を次に示します。
<%@ Page Language="VB" %> <%@ Import Namespace="Samples.AspNet.VB.Controls" %> <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 = "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>
<%@ Page Language="C#"%> <%@ Import Namespace="Samples.AspNet.CS.Controls" %> <script runat="server"> public void Page_Load(Object sender, EventArgs e) { // Define the resource name and type. String rsname = "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>
プログラムによって WebResourceAttribute メタデータ属性を適用し、提供されるリソースについてアセンブリにマークを付ける方法を次のコード例に示します。
Imports Microsoft.VisualBasic Imports System Imports System.Web Imports System.Web.UI Imports System.Security.Permissions <Assembly: WebResource("script_include.js", "application/x-javascript")> Namespace Samples.AspNet.VB.Controls <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _ Public Class ClientScriptResourceLabel ' Put code here that would ' go into the private assembly. End Class End Namespace
using System; using System.Web; using System.Web.UI; using System.Security.Permissions; [assembly: WebResource("script_include.js", "application/x-javascript")] namespace Samples.AspNet.CS.Controls { [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)] public class ClientScriptResourceLabel { // Put code here that would // go into the private assembly. } }
この例では、次のような内容の Script_include.js という名前の JavaScript ファイルが必要です。
ClientScriptResourceLabel クラスが含まれる Samples.AspNet.CS.Controls アセンブリ内に Script_include.js ファイルをリソースとしてコンパイルします。
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- ClientScriptManager.RegisterClientScriptResource メソッドのページへのリンク