ScriptManager.RegisterClientScriptBlock 메서드

정의

ScriptManager 컨트롤 내에 있는 컨트롤과 함께 사용할 UpdatePanel 컨트롤을 사용하여 클라이언트 스크립트 블록을 등록한 후 페이지에 스크립트 블록을 추가합니다.

오버로드

RegisterClientScriptBlock(Control, Type, String, String, Boolean)

ScriptManager 컨트롤 내에 있는 컨트롤과 함께 사용할 UpdatePanel 컨트롤을 사용하여 클라이언트 스크립트 블록을 등록한 후 페이지에 스크립트 블록을 추가합니다.

RegisterClientScriptBlock(Page, Type, String, String, Boolean)

ScriptManager 컨트롤 내에 있는 컨트롤과 함께 사용할 UpdatePanel 컨트롤을 사용하여 클라이언트 스크립트 블록을 등록한 후 페이지에 스크립트 블록을 추가합니다.

RegisterClientScriptBlock(Control, Type, String, String, Boolean)

ScriptManager 컨트롤 내에 있는 컨트롤과 함께 사용할 UpdatePanel 컨트롤을 사용하여 클라이언트 스크립트 블록을 등록한 후 페이지에 스크립트 블록을 추가합니다.

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

매개 변수

control
Control

클라이언트 스크립트 블록을 등록하는 컨트롤입니다.

type
Type

클라이언트 스크립트 블록의 형식입니다. 이 매개 변수는 일반적으로 typeof 연산자(C#) 또는 GetType 연산자(Visual Basic)로 스크립트를 등록하는 컨트롤의 형식을 검색하여 지정됩니다.

key
String

스크립트 블록의 고유한 식별자입니다.

script
String

스크립트입니다.

addScriptTags
Boolean

스크립트 블록을 true<script> 태그로 묶으면 </script>이고, 그렇지 않으면 false입니다.

예외

클라이언트 스크립트 블록 typenull인 경우

또는 스크립트 블록을 등록하는 컨트롤이 null인 경우

스크립트 블록을 등록하는 컨트롤이 페이지의 컨트롤 트리에 있지 않은 경우

예제


<%@ 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">
    protected void Page_PreRender(object sender, EventArgs e)
    {
        string script = @"
        function ToggleItem(id)
          {
            var elem = $get('div'+id);
            if (elem) 
            {
              if (elem.style.display != 'block') 
              {
                elem.style.display = 'block';
                elem.style.visibility = 'visible';
              } 
              else
              {
                elem.style.display = 'none';
                elem.style.visibility = 'hidden';
              }
            }
          }
        ";

        ScriptManager.RegisterClientScriptBlock(
            this,
            typeof(Page),
            "ToggleScript",
            script,
            true);
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ScriptManager RegisterClientScriptInclude</title>
</head>
<body>
    <form id="Form1" runat="server">
        <div>
            <br />
            <asp:ScriptManager ID="ScriptManager1" 
                                 EnablePartialRendering="true"
                                 runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" 
                               UpdateMode="Conditional"
                               runat="server">
                <ContentTemplate>
                    <asp:XmlDataSource ID="XmlDataSource1"
                                       DataFile="~/App_Data/Contacts.xml"
                                       XPath="Contacts/Contact"
                                       runat="server"/>
                    <asp:DataList ID="DataList1" DataSourceID="XmlDataSource1"
                        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
                        BorderWidth="1px" CellPadding="3" GridLines="Horizontal"
                        runat="server">
                        <ItemTemplate>
                            <div style="font-size:larger; font-weight:bold; cursor:pointer;" 
                                 onclick='ToggleItem(<%# Eval("ID") %>);'>
                                <span><%# Eval("Name") %></span>
                            </div>
                            <div id='div<%# Eval("ID") %>' 
                                 style="display: block; visibility: visible;">
                                <span><%# Eval("Company") %></span>
                                <br />
                                <a href='<%# Eval("URL") %>' 
                                   target="_blank" 
                                   title='<%# Eval("Name", "Link to the {0} Web site") %>'>
                                   <%# Eval("URL") %></a>
                                </asp:LinkButton>
                                <hr />
                            </div>
                        </ItemTemplate>
                        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                        <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                        <AlternatingItemStyle BackColor="#F7F7F7" />
                        <ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                    </asp:DataList>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </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_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim script As String
        script = _
        "function ToggleItem(id)" & _
        "  {" & _
        "    var elem = $get('div'+id);" & _
        "    if (elem)" & _
        "    {" & _
        "      if (elem.style.display != 'block') " & _
        "      {" & _
        "        elem.style.display = 'block';" & _
        "        elem.style.visibility = 'visible';" & _
        "      } " & _
        "      else" & _
        "      {" & _
        "        elem.style.display = 'none';" & _
        "        elem.style.visibility = 'hidden';" & _
        "      }" & _
        "    }" & _
        "  }"
        
        ScriptManager.RegisterClientScriptBlock( _
            Me, _
            GetType(Page), _
            "ToggleScript", _
            script, _
            True)

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ScriptManager RegisterClientScriptInclude</title>
</head>
<body>
    <form id="Form1" runat="server">
        <div>
            <br />
            <asp:ScriptManager ID="ScriptManager1" 
                                 EnablePartialRendering="true"
                                 runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" 
                               UpdateMode="Conditional"
                               runat="server">
                <ContentTemplate>
                    <asp:XmlDataSource ID="XmlDataSource1"
                                       DataFile="~/App_Data/Contacts.xml"
                                       XPath="Contacts/Contact"
                                       runat="server"/>
                    <asp:DataList ID="DataList1" DataSourceID="XmlDataSource1"
                        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
                        BorderWidth="1px" CellPadding="3" GridLines="Horizontal"
                        runat="server">
                        <ItemTemplate>
                            <div style="font-size:larger; font-weight:bold; cursor:pointer;" 
                                 onclick='ToggleItem(<%# Eval("ID") %>);'>
                                <span><%# Eval("Name") %></span>
                            </div>
                            <div id='div<%# Eval("ID") %>' 
                                 style="display: block; visibility: visible;">
                                <span><%# Eval("Company") %></span>
                                <br />
                                <a href='<%# Eval("URL") %>' 
                                   target="_blank" 
                                   title='<%# Eval("Name", "Link to the {0} Web site") %>'>
                                   <%# Eval("URL") %></a>
                                </asp:LinkButton>
                                <hr />
                            </div>
                        </ItemTemplate>
                        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                        <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
                        <AlternatingItemStyle BackColor="#F7F7F7" />
                        <ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                    </asp:DataList>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>
<Contacts>
    <Contact id="1" 
             Name="Aaber, Jesper" 
             Company="A. Data Corporation" 
             URL="http://www.adatum.com/"/>
    <Contact id="2" 
             Name="Canel, Fabrice" 
             Company="Coho Winery" 
             URL="http://www.cohowinery.com/"/>
    <Contact id="3" 
             Name="Heloo, Waleed" 
             Company="Contoso, Ltd" 
             URL="http://www.contoso.com/"/>
    <Contact id="4" 
             Name="Rovik, Dag" 
             Company="Wingtip Toys" 
             URL="http://www.wingtiptoys.com/"/>
</Contacts>

설명

이 메서드를 RegisterClientScriptBlock 사용하여 부분 페이지 렌더링과 호환되고 Microsoft Ajax 라이브러리 종속성이 없는 클라이언트 스크립트 블록을 등록합니다. 이 메서드를 사용하여 등록된 클라이언트 스크립트 블록은 업데이트되는 컨트롤 내에 있는 컨트롤을 나타내는 경우에만 control 페이지로 UpdatePanel 전송됩니다. 비동기 포스트백이 발생할 때마다 스크립트 블록을 등록하려면 이 메서드의 오버로드를 RegisterClientScriptBlock(Page, Type, String, String, Boolean) 사용합니다.

부분 페이지 업데이트와 관련이 없는 스크립트 블록을 등록하고 초기 페이지 렌더링 중에 스크립트 블록을 한 번만 등록하려는 경우 클래스의 메서드를 ClientScriptManager 사용합니다RegisterClientScriptBlock. 페이지의 속성에서 개체에 대한 참조 ClientScriptManagerClientScript 가져올 수 있습니다.

true경우 addScriptTags 메서드는 RegisterClientScriptBlock 스크립트 블록 주위에 태그를 추가 <script> 합니다. 특정 <script> 태그의 특성을 설정하려는 경우와 같이 태그를 직접 만들 <script> 려는 경우 전달 false 합니다. 이 false 경우 매개 변수에 script 여러 스크립트 블록이 포함되어 있으면 addScriptTags 예외가 throw됩니다.

이 메서드는 RegisterClientScriptBlock 열기 <form> 태그 뒤의 페이지에 스크립트 블록을 추가합니다. 스크립트 블록은 등록된 순서와 동일한 순서로 출력되도록 보장되지 않습니다. 스크립트 블록의 순서가 중요한 경우 스크립트 블록을 단일 문자열(예: 개체 사용 StringBuilder )에 연결한 다음 단일 클라이언트 스크립트 블록으로 등록합니다.

추가 정보

적용 대상

RegisterClientScriptBlock(Page, Type, String, String, Boolean)

ScriptManager 컨트롤 내에 있는 컨트롤과 함께 사용할 UpdatePanel 컨트롤을 사용하여 클라이언트 스크립트 블록을 등록한 후 페이지에 스크립트 블록을 추가합니다.

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

매개 변수

page
Page

클라이언트 스크립트 블록을 등록하는 페이지 개체입니다.

type
Type

클라이언트 스크립트 블록의 형식입니다. 이 매개 변수는 일반적으로 typeof 연산자(C#) 또는 GetType 연산자(Visual Basic)로 스크립트를 등록하는 컨트롤의 형식을 검색하여 지정됩니다.

key
String

스크립트 블록의 고유한 식별자입니다.

script
String

등록할 스크립트입니다.

addScriptTags
Boolean

스크립트 블록을 true<script> 태그로 묶으면 </script>이고, 그렇지 않으면 false입니다.

예외

스크립트 블록 typenull인 경우

또는 스크립트 블록을 등록하는 페이지가 null인 경우

설명

이 메서드를 사용하여 스크립트 블록을 등록하면 비동기 포스트백이 발생할 때마다 스크립트가 렌더링됩니다. 컨트롤이 업데이트될 때만 스크립트가 등록되도록 컨트롤 내부에 UpdatePanel 있는 컨트롤에 UpdatePanel 대한 스크립트 블록을 등록하려면 이 메서드의 오버로드를 RegisterClientScriptBlock(Control, Type, String, String, Boolean) 사용합니다.

부분 페이지 업데이트와 관련이 없는 스크립트 블록을 등록하고 초기 페이지 렌더링 중에 스크립트 블록을 한 번만 등록하려는 경우 클래스의 메서드를 ClientScriptManager 사용합니다RegisterClientScriptBlock. 페이지의 속성에서 개체에 대한 참조 ClientScriptManagerClientScript 가져올 수 있습니다.

추가 정보

적용 대상