Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
|
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
 
Old March 31st, 2010, 07:10 AM
Authorized User
 
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..
 
Old March 31st, 2010, 09:17 AM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

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..
 
Old April 1st, 2010, 04:11 AM
Authorized User
 
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
Default 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
 
Old April 1st, 2010, 01:23 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default 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("}");*/
 
Old April 4th, 2010, 02:21 PM
Authorized User
 
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
Default Thanks

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

Thanks alot I just tested the code and its perfect!!!
 
Old April 5th, 2010, 12:32 PM
Authorized User
 
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
Default 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);
 
Old April 5th, 2010, 08:02 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

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

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] + "]));");
 
Old April 6th, 2010, 03:51 PM
Authorized User
 
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
Default 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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convering a String Array to an Integer array nkrust C# 9 November 17th, 2010 12:02 PM
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
Field Array Validation in Javascript Asad Khan Apache Tomcat 1 September 4th, 2007 06:14 AM
Javascript Array Problem gherkin Javascript 0 May 8th, 2007 08:37 AM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.