ClientScriptManager.RegisterArrayDeclaration(String, String) 메서드

정의

배열 이름 및 배열 값을 사용하여 JavaScript 배열 선언을 Page 개체에 등록합니다.

public:
 void RegisterArrayDeclaration(System::String ^ arrayName, System::String ^ arrayValue);
public void RegisterArrayDeclaration(string arrayName, string arrayValue);
member this.RegisterArrayDeclaration : string * string -> unit
Public Sub RegisterArrayDeclaration (arrayName As String, arrayValue As String)

매개 변수

arrayName
String

등록할 배열 이름입니다.

arrayValue
String

등록할 배열 값 또는 값입니다.

예외

arrayNamenull입니다.

예제

다음 코드 예제에서는 및 RegisterHiddenField 메서드의 RegisterArrayDeclaration 사용을 보여 줍니다. 이 예제에서는 배열과 숨겨진 값을 등록하고 단추의 이벤트를 정의 OnClick 하여 배열의 <input> 두 값과 숨겨진 값의 합계를 계산합니다.

<%@ 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 array name and values.
    String arrName = "MyArray";
    String arrValue = "\"1\", \"2\", \"text\"";
    
    // Define the hidden field name and initial value.
    String hiddenName = "MyHiddenField";
    String hiddenValue = "3";
    
    // Define script name and type.
    String csname = "ConcatScript";
    Type cstype = this.GetType();
        
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Register the array with the Page class.
    cs.RegisterArrayDeclaration(arrName, arrValue);

    // Register the hidden field with the Page class.
    cs.RegisterHiddenField(hiddenName, hiddenValue);

    // Check to see if the  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='Sum = ' + ");
      cstext.Append("(parseInt(" + arrName + "[0])+");
      cstext.Append("parseInt(" + arrName + "[1])+");
      cstext.Append("parseInt(" + Form1.Name + "." + hiddenName + ".value));} </");
      cstext.Append("script>");
      cs.RegisterClientScriptBlock(cstype, csname, cstext.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" 
            onclick="DoClick()" 
            value="Run Script" />
     </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 array name and values.
    Dim arrName As String = "MyArray"
    Dim arrValue As String = """1"", ""2"", ""text"""
    
    ' Define the hidden field name and initial value.
    Dim hiddenName As String = "MyHiddenField"
    Dim hiddenValue As String = "3"
    
    ' Define script name and type.
    Dim csname As String = "ConcatScript"
    Dim cstype As Type = Me.GetType()
        
    ' Get a ClientScriptManager reference from the Page class.
    Dim cs As ClientScriptManager = Page.ClientScript

    ' Register the array with the Page class.
    cs.RegisterArrayDeclaration(arrName, arrValue)
    
    ' Register the hidden field with the Page class.
    cs.RegisterHiddenField(hiddenName, hiddenValue)

    ' Check to see if the  script is already registered.
    If (Not cs.IsClientScriptBlockRegistered(cstype, csname)) Then
      Dim cstext As StringBuilder = New StringBuilder()
      cstext.Append("<script type=""text/javascript\""> function DoClick() {")
      cstext.Append("Form1.Message.value='Sum = ' + ")
      cstext.Append("(parseInt(" + arrName + "[0])+")
      cstext.Append("parseInt(" + arrName + "[1])+")
      cstext.Append("parseInt(" + Form1.Name + "." + hiddenName + ".value));} </")
      cstext.Append("script>")
      cs.RegisterClientScriptBlock(cstype, csname, cstext.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" 
            onclick="DoClick()" 
            value="Run Script" />
     </form>
  </body>
</html>

설명

RegisterArrayDeclaration 등록된 배열이 매개 변수에 지정된 arrayName 이름과 같은 이름으로 존재하는지 확인하고, 있는 경우 매개 변수에 arrayValue 지정된 값을 추가합니다. 기본 스토리지 메커니즘은 기본 스토리지 메커니즘을 ArrayList기반으로 하므로 중복이 허용됩니다. 매개 변수와 이름이 arrayName 같은 등록된 배열이 없으면 생성되고 매개 변수의 값이 arrayValue 추가됩니다.

결과 JavaScript 배열에 문자열 리터럴을 사용하려면 매개 변수에 작은따옴표(') 또는 이스케이프된 큰따옴표(\") arrayValue 를 포함합니다. 매개 변수의 arrayValue 값은 단일 요소여야 합니다. 둘 이상의 값을 배열에 추가해야 하는 경우 메서드를 사용하여 여러 번 호출합니다 RegisterArrayDeclaration .

적용 대상

추가 정보