简体   繁体   English

为什么我不能将数据从C#发送到asp.net中的Javascript文件?

[英]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.

相关问题 为什么我无法从C#asp.net传递的javascript中的列表中获取值 - Why can't I get the value from list in javascript which passed from C# asp.net 如何使用asp.net将值从C#发送到javascript - How do I send my values from c# to javascript with asp.net SMTP服务器无法从托管c#asp.net发送电子邮件 - SMTP server can't send emails from hosting c# asp.net 我如何在ex.html c#中使用extern html文件作为附件并将其发送到电子邮件地址? - How I can use a extern html file for a attachment in asp.net c# and send it to a email address? 如何将对象从C#WebApp发送到ASP.NET WebAPI - how can I send object from c# webapp to asp.net webapi 如何将希伯来语文本从 C# ASP.NET 参数发送到 SQL Server? - How can I send Hebrew text to SQL Server from a C# ASP.NET parameter? 如何从C#Asp.Net方法发送HTTP Post? - How can I send a HTTP Post from a C# Asp.Net method? 为什么我不能在我的代码asp.net c#中使用app_code中的代码文件 - why I can't use code files from app_code in my code asp.net c# 无法从asp.net中的c#获取javascript中的数据 - not able to get data in javascript from c# in asp.net 来自 asp.net C# 页面的外部 javascript 文件 - external javascript file from asp.net C# page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM