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示します。 この例では、配列と非表示の値を登録し、配列の <input> 2 つの値と非表示の値の合計を計算するボタンのイベントを定義OnClickします。

<%@ 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 値は、1 つの要素である必要があります。 配列に複数の値を追加する必要がある場合は、 メソッドを使用して複数の呼び出しを RegisterArrayDeclaration 行います。

適用対象

こちらもご覧ください