p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Professionals (http://p2p.wrox.com/forumdisplay.php?f=352)
-   -   I am have been trying to assign a c# array value to a javascript array (http://p2p.wrox.com/showthread.php?t=78778)

ysfkay March 31st, 2010 07:10 AM

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...

PeterPeiGuo March 31st, 2010 09:17 AM

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);

ysfkay April 1st, 2010 04:11 AM

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

PeterPeiGuo April 1st, 2010 01:23 PM

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("}");*/


ysfkay April 4th, 2010 02:21 PM

Thanks
 
Thanks alot I just tested the code and its perfect!!!

ysfkay April 4th, 2010 02:28 PM

Thanks
 
Thanks alot I just tested the code and its perfect!!!

ysfkay April 5th, 2010 12:32 PM

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);

PeterPeiGuo April 5th, 2010 08:02 PM

With IE8, you should be able to:
1) View the exact script that was generated;
2) Locate the exact line that failed.

PeterPeiGuo April 6th, 2010 02:22 PM

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] + "]));");

ysfkay April 6th, 2010 03:51 PM

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.


All times are GMT -4. The time now is 01:48 PM.

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