 |
| ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 31st, 2010, 07:10 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
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..
|
|

March 31st, 2010, 09:17 AM
|
|
Friend of Wrox
|
|
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
|
|
Actually you didn't assign a C# array to javascript array, or assign a C# integer to a javascript integer. What you did was build a string in C#, which should contain a piece of valid javascript, and then register it with ClientScriptManager.
The way you did is hard to read and hard to debug. The best way is to define your javascript function in its own . js file, or between <script></script>. In your C# code, you simply say:
Code:
cs.RegisterClientScriptBlock(t, s2, "someJavascriptFunction()", true);
Last edited by PeterPeiGuo; March 31st, 2010 at 09:21 AM..
|
|

April 1st, 2010, 04:11 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Thanks
Thanks for the reply,
what I am trying to do actually is to retrieve some database info which are all integers store them in an array, then use them to create a html table dynamically.
so I am only using this for a test,
I thought that I assigned the c# array to javascript array when I did this in the code above.
int[] r={1,2,3,4,5,6};
sb.Append("function n(){ for("+ i +" = "+ 0 + ";"+ i +" <" + r.Length + ";" + i + " ++) alert(" +r[i]+");}");
and for the integer, I did this in the code above:
int n=6;
sb.Append("function n(){var arr=" + n+ ";alert(arr); }");
Please I need you to be more specific.thanks
|
|

April 1st, 2010, 01:23 PM
|
|
Friend of Wrox
|
|
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
|
|
couple of ways
Code:
//one way
sb.Append("function n(){r = [2, 4, 6, 8, 10, 12]; for(i=0;i<r.length ;i++) alert(r[i]);}");
//another way
/*sb.Append("function n(){");
for (int i = 0; i < r.Length; i++)
{
sb.Append("alert(" + r[i] + ");");
}
sb.Append("}");*/
|
|

April 4th, 2010, 02:21 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Thanks
Thanks alot I just tested the code and its perfect!!!
|
|

April 4th, 2010, 02:28 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Thanks
Thanks alot I just tested the code and its perfect!!!
|
|

April 5th, 2010, 12:32 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
I need some help here, thansks...
The code u wrote works,
I have now tried to apply it to create a html table dynamically using the array values as the html table data.
But the problem is in the C# for loops, where I use the c# outer and inner for loops' control variables i and j to index
the javascripts' DOM table methods.
When I click the button that calls the javascript method "n()", I get the
run time error "object expected", and this part of the button declaration is highlited "onclick="n();" "
I'd appreciate some explanation if possible, thanks.
here is the portion of my default.aspx.cs page
//////////////////////
//U may skip the following 6 lines of javascript-DOM that create the table and the table body
sb.Append("function n(){");
sb.Append("var oTable=document.createElement('table');");
sb.Append("oTable.setAttribute('border','1');");
sb.Append("oTable.setAttribute('width','100%');");
sb.Append("var oTBody=document.createElement('tbody');");
sb.Append("oTable.appendChild(oTBody);");
////////////////////
//this is the problem area
int x=0;
int[] r = { 1, 2, 3, 4, 5, 6,7,8,9,10 };
for (int i = 0; i < 5; i++)
{
sb.Append("oTBody.insertRow("+i+");");
for (int j = 0; j < 2; j++)
{
sb.Append("oTBody.rows["+i+"].insertCell("+j+");");
sb.Append(" oTBody.rows[" + i + "].cells[" + j + "].appendChild(document.createTextNode(" + r[x] + "]));");
sb.Append(" document.body.appendChild(oTable);");
++x;
}
}
sb.Append("}");
cs.RegisterClientScriptBlock(t, s2, sb.ToString(), true);
|
|

April 5th, 2010, 08:02 PM
|
|
Friend of Wrox
|
|
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
|
|
With IE8, you should be able to:
1) View the exact script that was generated;
2) Locate the exact line that failed.
|
|

April 6th, 2010, 02:22 PM
|
|
Friend of Wrox
|
|
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
|
|
Your generated javascript contains lines like the following one, and ()[] are mismatched:
Code:
oTBody.rows[0].cells[0].appendChild(document.createTextNode(1]));
This is generated by the following line, which is easy to fix:
Code:
sb.Append(" oTBody.rows[" + i + "].cells[" + j + "].appendChild(document.createTextNode(" + r[x] + "]));");
|
|

April 6th, 2010, 03:51 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Thanks
Thanks that solves it.
The table is created as I click the button
but the table only stays for about half a second then it disappears,
its like the generated table reloads the page and this courses the
table to disappear.
am still trying to solve it tho...
Any Ideas? thanks...
I have next to no experience with javascript.
|
|
 |