View Single Post
  #1 (permalink)  
Old March 31st, 2010, 07:10 AM
ysfkay ysfkay is offline
Authorized User
Points: 343, Level: 6
Points: 343, Level: 6 Points: 343, Level: 6 Points: 343, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
Default I am have been trying to assign a c# array value to a javascript array

I have been trying do assign the values of a c sharp array
to a java script array.

I tried doing this with a simple integer and it worked as follows


int n=6;
int i;
Type t=this.GetType();
string skey="key";
string s2="sSr";





ClientScriptManager cs = Page.ClientScript;


if (!cs.IsClientScriptBlockRegistered(t, skey))
{
StringBuilder sb = new StringBuilder();

sb.Append("function n(){var arr=" + n+ ";alert(arr); }");

cs.RegisterClientScriptBlock(t, s2, sb.ToString(), true);

}

the code above Outputs 6


But if I try the same thing, but this time using an array, this time I get a javascript error saying "cannot assign to a number"

int[] r = { 1, 2, 3, 4, 5, 6 };

if (!cs.IsClientScriptBlockRegistered(t, skey))
{
StringBuilder sb = new StringBuilder();

sb.Append("function n(){ for("+i+"="+0+";"+i+"<"+r.Length+";"+i+"++)alert(" +r[i]+");}");

cs.RegisterClientScriptBlock(t, s2, sb.ToString(), true);

}

This code stops when the javascript code is executed.
I really appreciate ur contribution, thanks...

Last edited by ysfkay; March 31st, 2010 at 07:14 AM..