ClientScriptManager.RegisterClientScriptResource(Type, String) 메서드

정의

형식 및 리소스 이름을 사용하여 클라이언트 스크립트 리소스 Page 를 개체에 등록합니다.

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

매개 변수

type
Type

등록할 클라이언트 스크립트 리소스의 형식입니다.

resourceName
String

등록할 클라이언트 스크립트 리소스의 이름입니다.

예외

클라이언트 리소스 유형은 .입니다 null.

-또는-

클라이언트 리소스 이름은 .입니다 null.

-또는-

클라이언트 리소스 이름의 길이는 0입니다.

예제

다음 코드 예제에서는 메서드의 사용을 보여 줍니다 RegisterClientScriptResource .

<%@ 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 하여 제공될 리소스에 대한 어셈블리를 표시하는 방법을 보여 줍니다.

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

이 예제에는 다음 내용이 포함된 JavaScript Script_include.js파일이 필요합니다.

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

파일을 클래스가 Script_include.js 포함된 어셈블리의 Samples.AspNet.CS.Controls 리소스로 컴파일합니다 ClientScriptResourceLabel .

설명

RegisterClientScriptResource 메서드는 WebResource.axd HTTP 처리기를 통해 어셈블리에서 컴파일된 리소스에 액세스할 때 사용됩니다. 이 메서드는 RegisterClientScriptResource 개체에 스크립트를 Page 등록하고 중복 스크립트를 방지합니다. 이 메서드는 리소스 URL의 내용을 요소 블록으로 <script> 래핑합니다.

적용 대상

추가 정보