ClientScriptManager.RegisterClientScriptBlock 메서드

정의

클라이언트 스크립트를 Page 개체에 등록합니다.

오버로드

RegisterClientScriptBlock(Type, String, String)

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

RegisterClientScriptBlock(Type, String, String, Boolean)

형식, 키, 스크립트 리터럴 및 스크립트 태그를 추가할지 여부를 나타내는 부울 값을 사용하여 클라이언트 스크립트를 Page 개체에 등록합니다.

RegisterClientScriptBlock(Type, String, String)

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

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

매개 변수

type
Type

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

key
String

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

script
String

등록할 클라이언트 스크립트 리터럴입니다.

예제

다음 코드 예제에서는 RegisterClientScriptBlock 메서드.

<%@ Page Language="C#"%>
<!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 name and type of the client script on the page.
    String csName = "ButtonClickScript";
    Type csType = this.GetType();

    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(csType, csName))
    {
      StringBuilder csText = new StringBuilder();
      csText.Append("<script type=\"text/javascript\"> function DoClick() {");
      csText.Append("Form1.Message.value='Text from client script.'; }");
      csText.Append("</script>");
      cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
    }
  }
</script>
<html  >
  <head>
    <title>RegisterClientScriptBlock Example</title>
  </head>
  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>
<%@ Page Language="VB"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Public Sub Page_Load(ByVal sender As [Object], ByVal e As EventArgs)
        ' Define the name and type of the client script on the page. 
        Dim csName As [String] = "ButtonClickScript"
        Dim csType As Type = Me.[GetType]()
        
        ' Get a ClientScriptManager reference from the Page class. 
        Dim cs As ClientScriptManager = Page.ClientScript
        
        ' Check to see if the client script is already registered. 
        If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
            Dim csText As New StringBuilder()
            csText.Append("<script type=""text/javascript""> function DoClick() {")
            csText.Append("Form1.Message.value='Text from client script.'; }")
            csText.Append("</script>")
            cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
        End If
    End Sub
</script>
<html  >
  <head>
    <title>RegisterClientScriptBlock Example</title>
  </head>
  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>

설명

클라이언트 스크립트는 해당 키 및 해당 형식에 따라 고유 하 게 식별 됩니다. 동일한 키와 형식을 사용 하 여 스크립트에는 중복으로 간주 됩니다. 지정 된 형식 및 키 쌍을 사용 하 여 하나의 스크립트 페이지를 사용 하 여 등록할 수 있습니다. 이미 등록 되어 있는 스크립트를 등록 하는 동안 스크립트의 중복을 만들지 않습니다.

호출 된 IsClientScriptBlockRegistered 메서드를 지정된 된 키 및 유형 쌍을 사용 하 여 클라이언트 스크립트를 이미 등록 되어 있는지 여부를 확인 하 고 불필요 하 게 스크립트를 추가 하려는 시도 방지 합니다.

이 오버 로드는 RegisterClientScriptBlock 메서드를 확인 해야 스크립트에서 제공 되는지 확인 합니다 script 매개 변수에서 래핑됩니다는 <script> 요소 블록.

RegisterClientScriptBlock 메서드 스크립트 블록을 렌더링된 된 페이지의 맨 위에 추가 합니다. 등록 된 순서에 출력 되는 스크립트 블록 보장이 없습니다. 사용 하 여 스크립트 블록의 순서가 중요 한 경우는 StringBuilder 개체를 단일 문자열에서 스크립트를 함께 수집 및 단일 클라이언트 스크립트 블록의 모든 등록 합니다.

추가 정보

적용 대상

RegisterClientScriptBlock(Type, String, String, Boolean)

형식, 키, 스크립트 리터럴 및 스크립트 태그를 추가할지 여부를 나타내는 부울 값을 사용하여 클라이언트 스크립트를 Page 개체에 등록합니다.

public:
 void RegisterClientScriptBlock(Type ^ type, System::String ^ key, System::String ^ script, bool addScriptTags);
public void RegisterClientScriptBlock (Type type, string key, string script, bool addScriptTags);
member this.RegisterClientScriptBlock : Type * string * string * bool -> unit
Public Sub RegisterClientScriptBlock (type As Type, key As String, script As String, addScriptTags As Boolean)

매개 변수

type
Type

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

key
String

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

script
String

등록할 클라이언트 스크립트 리터럴입니다.

addScriptTags
Boolean

스크립트 태그를 추가할지 여부를 나타내는 부울 값입니다.

예외

클라이언트 스크립트 블록 형식이 null인 경우

예제

다음 코드 예제에서는 RegisterClientScriptBlock 메서드. 합니다 addScriptTags 매개 변수는 설정 true 시작 태그와 닫는 태그 스크립트에 포함 되므로 script 매개 변수입니다.

<%@ Page Language="C#"%>
<!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 name and type of the client scripts on the page.
    String csname1 = "PopupScript";
    String csname2 = "ButtonClickScript";
    Type cstype = this.GetType();
        
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
      String cstext1 = "alert('Hello World');";
      cs.RegisterStartupScript(cstype, csname1, cstext1, true);
    }

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
    {
      StringBuilder cstext2 = new StringBuilder();
      cstext2.Append("<script type=\"text/javascript\"> function DoClick() {");
      cstext2.Append("Form1.Message.value='Text from client script.'} </");
      cstext2.Append("script>");
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>
<%@ Page Language="VB" %>

<!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 name and type of the client scripts on the page.
    Dim csname1 As String = "PopupScript"
    Dim csname2 As String = "ButtonClickScript"
    Dim cstype As Type = Me.GetType()
    
    ' Get a ClientScriptManager reference from the Page class.
    Dim cs As ClientScriptManager = Page.ClientScript

    ' Check to see if the startup script is already registered.
    If (Not cs.IsStartupScriptRegistered(cstype, csname1)) Then
      
      Dim cstext1 As String = "alert('Hello World');"
      cs.RegisterStartupScript(cstype, csname1, cstext1, True)
      
    End If
    
    ' Check to see if the client script is already registered.
    If (Not cs.IsClientScriptBlockRegistered(cstype, csname2)) Then
      
      Dim cstext2 As New StringBuilder()
            cstext2.Append("<script type=""text/javascript""> function DoClick() {")
      cstext2.Append("Form1.Message.value='Text from client script.'} </")
      cstext2.Append("script>")
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), False)
      
    End If
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message" /> <input type="button" value="ClickMe" onclick="DoClick()" />
     </form>
  </body>
</html>

설명

클라이언트 스크립트는 해당 키 및 해당 형식에 따라 고유 하 게 식별 됩니다. 동일한 키와 형식을 사용 하 여 스크립트에는 중복으로 간주 됩니다. 지정 된 형식 및 키 쌍을 사용 하 여 하나의 스크립트 페이지를 사용 하 여 등록할 수 있습니다. 이미 등록 되어 있는 스크립트를 등록 하는 동안 스크립트의 중복을 만들지 않습니다.

호출 된 IsClientScriptBlockRegistered 지정된 된 키 및 유형 쌍을 사용 하 여 클라이언트 스크립트를 이미 등록 되어 있는지 여부를 확인 하는 방법입니다. 이 스크립트를 추가 하는 동안 불필요 하 게 피할 수 있습니다.

이 오버 로드는 RegisterClientScriptBlock 메서드를 지정할 수 있습니다. 스크립트의 제공 여부를 합니다 script 매개 변수를 사용 하 여 래핑됩니다를 <script> 를 사용 하 여 요소 블록을 addScriptTags 매개 변수입니다. 설정 addScriptTagstrue 스크립트 태그를 자동으로 추가 되어야 함을 나타냅니다.

RegisterClientScriptBlock 메서드 스크립트 블록을 렌더링된 된 페이지의 맨 위에 추가 합니다. 등록 된 순서에 출력 되는 스크립트 블록 보장이 없습니다. 사용 하 여 스크립트 블록의 순서가 중요 한 경우는 StringBuilder 개체를 단일 문자열에서 스크립트를 함께 수집 및 단일 클라이언트 스크립트 블록의 모든 등록 합니다.

추가 정보

적용 대상