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> 요소 블록입니다.

적용 대상

추가 정보