Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 31st, 2010, 07:10 AM
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..
  #2 (permalink)  
Old March 31st, 2010, 09:17 AM
Friend of Wrox
 
Join Date: Dec 2008
Location: , , .
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..
  #3 (permalink)  
Old April 1st, 2010, 04:11 AM
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 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
  #4 (permalink)  
Old April 1st, 2010, 01:23 PM
Friend of Wrox
 
Join Date: Dec 2008
Location: , , .
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("}");*/
  #5 (permalink)  
Old April 4th, 2010, 02:21 PM
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 Thanks

Thanks alot I just tested the code and its perfect!!!
  #6 (permalink)  
Old April 4th, 2010, 02:28 PM
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 Thanks

Thanks alot I just tested the code and its perfect!!!
  #7 (permalink)  
Old April 5th, 2010, 12:32 PM
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 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);
  #8 (permalink)  
Old April 5th, 2010, 08:02 PM
Friend of Wrox
 
Join Date: Dec 2008
Location: , , .
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.
  #9 (permalink)  
Old April 6th, 2010, 02:22 PM
Friend of Wrox
 
Join Date: Dec 2008
Location: , , .
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] + "]));");
  #10 (permalink)  
Old April 6th, 2010, 03:51 PM
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 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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:40 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.