Tuesday, March 08, 2005

Registering Arrays In Client Side.

RegisterArrayDeclaration(arrayName, arrayValue)
If you need to create a client-side JavaScript Array object with some set values, use this method to add a value to a specific array. For example, when using validation controls in an ASP.NET Web page, an Array object (Page_Validators) is built that contains references to the set of validation controls on the page. When the form is submitted, this array is enumerated to check if the various validation controls are valid or not.

To add the values 1, 2, and 3 to a client-side Array object named FavoriteNumbers, you'd use the following server-side code:

RegisterArrayDeclaration("FavoriteNumbers", "1")
RegisterArrayDeclaration("FavoriteNumbers", "2")
RegisterArrayDeclaration("FavoriteNumbers", "3")

This code would emit the following client-side script:

<script language="javascript">
<!--
var FavoriteNumbers = new Array(1, 2, 3);
-->
</script>


Notice that each array value passed in must be a string; however, the client-side script rendered sets
the values of the Array object as the contents of the string. That is, if you wanted to create an Array with the string values "Scott" and "Jisun", you'd use:

RegisterArrayDeclaration("FavoriteFolks", "'Scott'")
RegisterArrayDeclaration("FavoriteFolks ", "'Jisun'")

Notice that the second input parameters are strings that contain 'Scott' and 'Jisun'—text delimited by a single apostrophe. This would render the following client-side script:

<script language="javascript">
<!--
var FavoriteFolks = new Array('Scott', 'Jisun');
-->
</script>