Problem accessing an array from a string which contains the name of the array

I am trying to access the elements of an array by using javascript on the client side.  The array is registered to the page using the ClientScript.RegisterArrayDeclaration function from the server side. The problem is that I don't know what the name of the array is going to be at design time because it is created dynamically from my server side script so I can't just have a line in my javascript code that says for example:

 

MyArray5[0]

 

I have to have a variable that turns out to be a string containing the name of the array using code similar to:

 

var myArrayName  = "MyArray" + siteKey[0]

 

however the problem is that I still can't access the Array object by saying

myArrayName[0] because myArrayName is a string containing the name of the array, not a reference to the array itself.  How can I obtain a reference to the array itself by using a string containing the name of the Array?  It's probably something simple that I'm missing but I don't know what to do.  document.getElementByID doesn't work and returns null since it's not a DOM object. Can anybody tell me how to access the elements of this array using javascript?

 

~Eric
 

0 Shaggy8 6/19/2008 6:50:42 PM

Well im not fan of RegisterArrayDeclaration but i can surely mention u easy way. Look at the example below. Create the Javascript string and then register whole block.

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Exampletitle>

head>

<body>

<form id="Form1" runat="server">

<input type="button" onclick="DoClick()" value="check" />

form>

body>

html>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim cstext As StringBuilder = New StringBuilder()

cstext.Append("var MyArray = new Array(""1"", ""2"", ""text"");")

cstext.Append(" function DoClick() {alert(MyArray);};")

ScriptManager.RegisterStartupScript(Me, Me.GetType, "array", cstext.ToString, True)

End Sub

 


Please mark as Answer if it helps u. Thanks!

Parth Patel
Techsture Technologies
Software Developer
Ahmedabad
0 Coool 6/20/2008 5:06:11 AM

Hi,

When any javascript code is not encapsulated within a function, it will execute as it gets read sequentially.
So, your arrays are likely defined later on in the markup (so the script is executing before the arrays are there).
Do something like this...

* 1st Method :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Page.ClientScript.RegisterArrayDeclaration("myArray", "1,2,3")

        Dim scriptString As String
        scriptString = "var i = 0;"
        scriptString += "for( i=0;i        scriptString += "{"
        scriptString += "alert(myArray[i]);"
        scriptString += "}"

        ScriptManager.RegisterStartupScript(Page, Page.GetType, "arrayScript", scriptString, True)

End Sub


* 2nd Method :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Page.ClientScript.RegisterArrayDeclaration("myArray", "1,2,3,4")

End Sub



    Untitled Page


   


   

  
   

   


   




Maulik Patel
MCTS, Software Engineer

Don't forget to click "Mark as Answer" on the post that helped you. This will give you point and help readers to know which post solved your issue and make their search easy.
0 Maulik 6/20/2008 6:59:04 AM
Reply:

(Thread closed)