[英]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. 我有一个用c#创建的类的列表,我必须将其值发送到javascript文件。 I have created a string in c# and put the values of the list in it:
我在c#中创建了一个字符串,并将列表的值放入其中:
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. GraphData是用于保存字符串的标签。
In the JavaScript file, I added: 在JavaScript文件中,我添加了:
$("#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
您在每个周期都将
i
和count都增加1,所以i
总是少于count
After you fix the problem with the infite loop, you need to call $('#GraphData').text()
; 用infite循环解决问题后,需要调用
$('#GraphData').text()
; because Label
in asp.net is rendered as a span
element. 因为asp.net中的
Label
呈现为span
元素。
I think here GraphData is the server control. 我认为这里GraphData是服务器控件。 to get this element use its ClientID
要获取此元素,请使用其ClientID
try this 尝试这个
$("# <%= GraphData.ClientID%> ").val(); $(“#<%= GraphData.ClientID%>”).val();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.