如何将 c# 数据表传递给 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19567990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to pass c# datatable to a javascript function
提问by undertree
I have these data in thhe codebehind and tried to pass it to javascript function in various format: array of list, json string but no way to get data by a javascript var object. Here the last format of data in the code behind:
我在代码隐藏中有这些数据,并尝试将其以各种格式传递给 javascript 函数:列表数组、json 字符串,但无法通过 javascript var 对象获取数据。这里是后面代码中最后一种数据格式:
List<string>[] array2 = new List<string>[listLenght];
array2.Initialize();
for (int ii = 0; ii < listLenght; ii++)
{
array2[ii] = new List<string>();
array2[ii].Add(Convert.ToString(dt.Rows[ii][5]));
array2[ii].Add(Convert.ToString(dt.Rows[ii][9]));
array2[ii].Add(Convert.ToString(dt.Rows[ii][10]));
array2[ii].Add(Convert.ToString(dt.Rows[ii][11]));
}
Then tried to call javascriot in this way:
然后尝试以这种方式调用javascriot:
string createArrayScript = string.Format("var array2 = [{0},{1},{2},{3}];", string.Join(",",",",",", array2));
but return an error: FormatException was unhandled by user code The index (zero based) must be greater than or equal to zero and less than the size of the list of topics.
但返回错误:用户代码未处理 FormatException 索引(从零开始)必须大于或等于零且小于主题列表的大小。
This is the call to the function: Page.ClientScript.RegisterStartupScript(this.GetType(), "registerPoiArray", createArrayScript, true);
这是对函数的调用: Page.ClientScript.RegisterStartupScript(this.GetType(), "registerPoiArray", createArrayScript, true);
Here the javascript var format:
这里是 javascript var 格式:
var markers = Poi;
var markers = [
{
"title": "via Andria, Roma",
"lat": 41.8902643,
"lng": 12.6638589,
"description": "fdsad"
},
{
"title": "via canosa, Roma",
"lat": 41.8838417,
"lng": 12.5438227,
"description": "fdsad"
},
{
"title": "via taranto, Milano",
"lat": 45.4383343,
"lng": 9.1505354,
"description": "fdsad"
},
{
"title": "via barletta, Roma",
"lat": 41.9102707,
"lng": 12.4580826,
"description": "fdsad"
}];
I can not pass this array in javascript. Help me please
我无法在 javascript 中传递这个数组。请帮帮我
回答by Saranya
You can create a custom class to represent the Datatable. Say if your data table has four columns, create a custom class which has 4 fields in it. Loop through the data table and convert it in to an array of objects of the custom class type.
您可以创建一个自定义类来表示数据表。假设您的数据表有四列,请创建一个包含 4 个字段的自定义类。循环遍历数据表并将其转换为自定义类类型的对象数组。
Finally you can serialize the array into json using the following code.
最后,您可以使用以下代码将数组序列化为 json。
JavaScriptSerializer js = new JavaScriptSerializer();
js.Serialize(data);
where data is the array of objects.
其中 data 是对象数组。
This is the technique i used..
这是我使用的技术..
Data table cannot be serialized easily to JSON directly. refer What's the best way to jSON serialize a .NET DataTable in WCF?
数据表不能轻易地直接序列化为 JSON。请参阅在 WCF 中 JSON 序列化 .NET DataTable 的最佳方法是什么?
回答by nothingisnecessary
You have to escape (double up) the curly brace when using String.Format if you want it to spit out the actual curly brace character.
如果您希望它吐出实际的花括号字符,则在使用 String.Format 时必须转义(加倍)花括号。
Example:
例子:
string createArrayScript =
string.Format("var array2 = [{{0}},{{1}},{{2}},{{3}}];",
...
回答by MattE
Just use NewtonSoft Json and call:
只需使用 NewtonSoft Json 并调用:
var JsonString = JsonConvert.SerializeObject(NameofDT, Formatting.Indented)
回答by unconnected
Try to use
尝试使用
System.Web.Helpers.Json.Encode
And change the structure you are getting.
并改变你得到的结构。
List<object> toEncode=new List<object>();
foreach (DataRow dr in dt.Rows){
Dictionary<string,string> element=new Dictionary<string,string>();
element.Add("title",dr["title"] as string);
//repeat for all neaded colums
toEncode.Add(element);
}
string jsonString=Json.Encode(toEncode);