.NET Framework Class Library
ClientScriptManagerRegisterArrayDeclaration Method

Registers a JavaScript array declaration with the Page object using an array name and array value.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Syntax
Public Sub RegisterArrayDeclaration ( _
	arrayName As [%$TOPIC/dfy0638f_en-us_VS_110_2_0_0_0_0%], _
	arrayValue As [%$TOPIC/dfy0638f_en-us_VS_110_2_0_0_0_1%] _
)
public void RegisterArrayDeclaration(
	[%$TOPIC/dfy0638f_en-us_VS_110_2_0_1_0_0%] arrayName,
	[%$TOPIC/dfy0638f_en-us_VS_110_2_0_1_0_1%] arrayValue
)
public:
void RegisterArrayDeclaration(
	[%$TOPIC/dfy0638f_en-us_VS_110_2_0_2_0_0%]^ arrayName, 
	[%$TOPIC/dfy0638f_en-us_VS_110_2_0_2_0_1%]^ arrayValue
)
member RegisterArrayDeclaration : 
        arrayName:[%$TOPIC/dfy0638f_en-us_VS_110_2_0_3_0_0%] * 
        arrayValue:[%$TOPIC/dfy0638f_en-us_VS_110_2_0_3_0_1%] -> unit

Parameters

arrayName
Type: SystemString

The array name to register.

arrayValue
Type: SystemString

The array value or values to register.

Exceptions
ExceptionCondition
ArgumentNullException

arrayName is .

Remarks

The RegisterArrayDeclaration checks to see whether a registered array exists with the same name as the name specified in the arrayName parameter and, if so, adds the values specified in the arrayValue parameter. Because the underlying storage mechanism is based on an ArrayList, duplicates are allowed. If a registered array with the same name as the arrayName parameter does not exist, it is created and the values in the arrayValue parameter added to it.

If you want string literals in the resulting JavaScript array, include single quotation marks (') or escaped double quotation marks (\") in the arrayValue parameter. The value of the arrayValue parameter should be a single element. If more than one value needs to be added to the array, make multiple calls using the RegisterArrayDeclaration method.

Examples

The following code example demonstrates the use of the RegisterArrayDeclaration and RegisterHiddenField methods. The example registers an array and a hidden value and defines the OnClick event of an <input> button to calculate the sum of two values of the array and the hidden value.

<%@ 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>
<%@ 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>
Version Information

.NET Framework

Supported in: 4.6, 4, 3.5, 3.0, 2.0
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.