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
asp.net.client-side 24353 articles. 1 followers. Follow

2 Replies
475 Views

Similar Articles

[PageSpeed] 7

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>Example</title>

</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<myArray.length; 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


<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
    </div>
    </form>

    <script type="text/javascript">

    window.onload = function()
        {
            //Now process your arrays
            var i = 0;
            for( i=0;i<myArray.length; i++ )
            {
                alert(myArray[i]);
            }
        }
       
    </script>

</body>
</html>


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:

Similar Artilces:

Problem accessing server side array values from javascript client side function
i faced the following problem ..i have an array that store the values of a resultset row under the server side coding.How do i retrieve them under my client side javascript function.I wrote the following coding under my client side function and it got an error.BSCStr is an array storing data from a resultset row which i did under the server side coding. From client side function: addr=addr + "&C" + i + "=" + <%=BSCStr[i]%>; First of all, you can't mix server-side and client-side code. The reason is that the protocol that is used to communicate betwee...

Problem accessing server side array values from client side javascript function
i faced the following problem ..i have an array that store the values of a resultset row under the server side coding.How do i retrieve them under my client side javascript function.I wrote the following coding under my client side function and it got an error.BSCStr is an array storing data from a resultset row which i did under the server side coding. From client side function: addr=addr + "&C" + i + "=" + <%=BSCStr[i]%>;...

Problem accessing a server side declared array from the client side javascript function.
I have an array that store the values of a resultset row under the server side coding.How do i retrieve them under my client side javascript function.I wrote the following coding under my client side function and it got an error.BSCStr is an array storing data from a resultset row which i did under the server side coding. From client side javascript function: addr=addr + "&C" + i + "=" + <%=BSCStr[i]%>; Hi wingshya, Did you still need help with this thread? Jason ConwayMCP, MCTS, Certified SCRUM Masterasp.net blog...

Arrays, Arrays, Arrays
I mainly come from a JScript classic ASP background and I'm liking both C#.net and JScript.net. Now I have to say... "What's happened to arrays?" They used to be so flexible and easy to use. Here are my quick questions... Am I allowed to create an array with a non specified length so that I can add to it? If not then how can I modify an array's length? Can I create an array with a named index eg myArray["apple"] = "tasty"; or is the hash table the equivalent for this now? The classic array methods and properties like pop(), push(), length etc... are ...

Accessing arrays of arrays
Hello, I was wondering how I would could access arrays of arrays. I would like to be able to get to (print, for now) the value in each nested array. Here is what I've been trying: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #!/usr/bin/perl @jon = qw/ "jon" "hansfelt" "123-1122" /; @ben = qw/ "ben" "jones" "222-1231" /; @marsha = qw/ "marsha" "padgett" "333-9087" /; @abe = qw/ "abe" "johnson" "421-4623" /; @address = qw/ $jon $marsha $ben $abe /; @ad...

problems parsing array or associative array (of strings) in from RenderScript
I can not find a way for my atlas server RendScript to contain a subnode representing an array or associative array and pass this information to the client object (successfully at least). Atlas seems to want a sophisticated object contained as array elements. Is there anyway to have an array (from RenderScript) parse easliy by atlas client object? The sample RendScript code here:  protected override void RenderScript(ScriptTextWriter writer, Control targetControl) {   Foo foo = targetControl as Foo;   FooExtenderProperties properties = GetTargetPro...

referring to an array in an array of arrays
How can I get at an array in an array of arrays? I'm creating the arrays using: $fComments[$i++] = [ @comments ]; Now I want to get back what I put in: print "$fComments[0]\n"; but this prints ARRAY(0x107671f0). How do I follow that pointer? (I really think we need a pre-beginners list... =) TIA. - B You can try something like this: print "@{$fComments[0]}\n"; Which will attempt to dereference the array at $fComments[0]. -----Original Message----- From: Bryan R Harris [mailto:Bryan_R_Harris@raytheon.com] Sent: Tue...

Concatenating arrays from arrays of arrays
Hello - I am trying to accumulate items in a flat list (array) by concatenating on to it the scalars contained in arrays which in turn are contained in arrays. The data is to be read in from a file rather than existing as literals in my code. Everything I try either crashes the computer or gives me an array of arrays instead of a flat list. I have been programming in Unicon which has a concatenate operator, but Perl makes very heavy weather of nested list management. Should I switch to Lisp, or is there a way of doing what I want to do in Perl? Regards _John Sampson_ On...

How to unshift to an array in an array of arrays?
How to unshift to an array in an array of arrays? > -----Original Message----- > From: Angerstein [mailto:ang@nmc-m.dtag.de] > Sent: Wednesday, August 28, 2002 6:12 AM > To: beginners@perl.org > Subject: How to unshift to an array in an array of arrays? > > > How to unshift to an array in an array of arrays? $ perl -d -e 1 ..... DB<1> @aoa = ([qw(foo bar baz)]) << create array of arrays DB<2> x @aoa 0 ARRAY(0x80f82dc) 0 'foo' 1 'bar' 2 'baz' DB<3> unshift @{$aoa[0]}, 'qu...

How to Access/Pass Server Side Array Variable at Another Web Form's Client Side
Hi All,I struck up with some intrested senario at my application development...hope some body will guide me or sends me some code snippets....Thanks in advance.Senario is like this.....in A.aspx (using C# .net 2.0 as code behind) I am going to have a array variable basically it is an TWO Dimensional array after populating the values in array I need to redirect/ reload another web form B.aspx, here i need to access the array in B.aspx client side JavaScript.How can I do this...Thanks AgainRaju Dont do a redirect but use server.transfer. That way the previous page will still hold its value, ...

Array of Array.
How is possible to declare a DYNAMIC array of array ? I need something like a dynamic hash table whose pages grow up dynamically too (each one can have different number of elements). In other words, a matrix that can be dynamic in both dimension. example: [1,2,3...] [3,4..] [5] [6,7,8,9...] [.] .. .. .. Have you considered using a DataStore? -- <hopethishelps/> Roy Kiesler [TeamSybase] SDN CodeXchange -- http://codexchange.sybase.com "Marco Meoni - News" <mmeoni@usl4.toscana.it> wrote in message news:401ba036$1@forums-2-dub... > How ...

Array of Arrays
Hi, I am looking for a way to create an array of arrays which needs to be passed to Oracle database to minimize the round trips to hte server. I was successful in sending simple arrays so far but no idea on how to create array of arrays in PB. I have used structures to simulate this but looks like I can't send the structure to Oracle. Anyone has any suggestions? Thanks, Anil Please don't cross-post. On 3 Mar 2005 14:02:47 -0800, Anil wrote: >Hi, > >I am looking for a way to create an array of arrays which >needs to be passed to Oracle database t...

Array of arrays
Hi I'm trying to return from a web service a array of arrays, one of the arrays is a array of objects, which I tested separately and it's working fine, and another array of integer and another of strings. When I run the web service I got this:You must implement a default accessor on System.Array because it inherits from ICollectionDefault accessor what that means? Thank you very much.  As Heinlein said "specialization is for insects" explore beyond the universe. Could you pls post the relevant code ? hi rushan thank you for the reply the code is here:  ...

[@array] and \@array
Given the case, my @array = (1,2,3,4); my $var1 = [@array]; my $var2 = \@array; What's the difference between $var1 and $var2? Are they all refered to @array? Thanks guys. On 3/22/07, Jm lists <practicalperl@gmail.com> wrote: > Given the case, > > my @array = (1,2,3,4); > my $var1 = [@array]; > my $var2 = \@array; > > What's the difference between $var1 and $var2? > Are they all refered to @array? > Thanks guys. $var2 is a reference to @array. $var1 is a reference to new array. Observe #!/usr/bin/perl use strict; use war...

Web resources about - Problem accessing an array from a string which contains the name of the array - asp.net.client-side

Highest Percentage Of Opera Mini Users Accessing Facebook? Macau
If you were asked to guess which country had the highest percentage of users of Opera mobile Web browser Opera Mini users accessing Facebook ...

International Users Accessing Facebook Places Through US VPN Accounts
By using a virtual private network (VPN) hosted in the United States, Facebook users from around the world are accessing Facebook Places. The ...

What are some alternatives to Yodlee for accessing bank information?
Clay Loveless , Founder, Jexy. Co-founder, Mashery. Founder, Jexy. Co-founder, Mashery.

Cloud Console - Accessing files in cloud storage for iPad on the iTunes App Store
Get Cloud Console - Accessing files in cloud storage on the App Store. See screenshots and ratings, and read customer reviews.

Meryl Streep On Accessing The Characters Within - YouTube
Meryl Streep talks about the importance of an actors work representing their ability. CONNECT WITH AFI: http://facebook.com/AmericanFilmInstitute ...

Accessing the Future Fund a sensible proposal, says super expert Daryl Dixon
As portfolios go, the Future Fund's asset allocation is concentrated in higher-risk investment, including equities and alternative assets.

Accessing my Super
I'm 57 - can I access my super?

Australia, US accessing Indonesian telephone data, leaked documents show
Newly disclosed documents from former US intelligence contractor Edward Snowden have revealed that Australian intelligence efforts against Indonesia ...

Euthanasia advocate Philip Nitschke detained at Melbourne Airport, accuses Customs officials of accessing ...
Euthanasia advocate Philip Nitschke has accused Customs officials of accessing hundreds of patients' sensitive records on his laptop after detaining ...

Frances Abbott scholarship: Sydney woman pleads guilty to accessing Whitehouse Institute records on PM's ...
A Sydney woman who leaked the student records of Tony Abbott's daughter pleads guilty. A Sydney woman who leaked the student records of Prime ...

Resources last updated: 1/10/2016 5:16:10 AM