Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass Array from C# to javascript

Status
Not open for further replies.

jennifercheng2008

Programmer
Jul 24, 2008
10
CA
I have a string array SchoolName[100] in .net aspx.cs file. Now I want to pass this array to javascript code. But I can't use following code:

var addresses=new Array();
for (var i=0; i <99; i++)
{
addresses= "<%=SchoolName %>";
}

If i is not defined in aspx.cs file, the error will show i is not defined there.
But if i is defined in aspx.cs file, any addresses will be assigned to the value of SchoolName[0].

I can assign values as following:

addresses[0]= "<%=SchoolName[0] %>";
addresses[1]= "<%=SchoolName[1] %>";
.
.
.
addresses[99]= "<%=SchoolName[99] %>";

But it is not good since SchoolName size should always change.

I would be appreciated if you may give me some advice.

Jennifer


 
from your original post, it doesn't look like you're looping over the SchoolName collection on the server side. is this true? the server side code is executed before any client side code.

you'd need to perform a loop on the server side and write out the javascript array, e.g. (note this is not necessarily syntactically correct C#, i'm not a C# guy)

var addresses = new Array();
<%
for (int x = 0; x < SchoolName.length; x++) {
System.out.write("addresses["+x+"]= '"+SchoolName[x]+"';");
}
%>




-jeff
lost: one sig, last seen here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top