简体   繁体   中英

Why can't I send data from c# to Javascript file in asp.net?

I have a list of a class created in c# that I have to send its values to a javascript file. I have created a string in c# and put the values of the list in it:

count = 0;
JString = "[";
for(i=0; i<x; i++)
{
    JString += "{Source:" + A[i] + ", Number:" + 3 + ", Target:" + B[i] + "},";
    count++;
}
JString = JString.Remove(JString.Length - 1, 1); //to remove the last ,
JString += "]";
GraphData.Text = "" + "var JString =" + JString + " ;" + "var count =" + count + " ;";

GraphData is a label to save the string.

In the JavaScript file, I added:

 $("#GraphData").val(); //to get the string sent

But it's not working this way. Am I doing something wrong?

Thanks in advance:)

for(i=0; i<count; i++)
{
    JString += "{Source:" + A[i] + ", Number:" + 3 + ", Target:" + B[i] + "},";
    count++;
}

This looks like an infinte loop. You're increasing both i and count by 1 in every cycle, so i will always be less than count

After you fix the problem with the infite loop, you need to call $('#GraphData').text() ; because Label in asp.net is rendered as a span element.

I think here GraphData is the server control. to get this element use its ClientID

try this

$("# <%= GraphData.ClientID%> ").val();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM