简体   繁体   English

如何将c#datatable传递给javascript函数

[英]how to pass c# datatable to a javascript function

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. 我在代码隐藏中有这些数据并试图以各种格式将它传递给javascript函数:list of array,json string但无法通过javascript var对象获取数据。 Here the last format of data in the code behind: 这里是代码背后的最后一种数据格式:

    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. 我无法在javascript中传递此数组。 Help me please 请帮帮我

You can create a custom class to represent the Datatable. 您可以创建一个自定义类来表示Datatable。 Say if your data table has four columns, create a custom class which has 4 fields in it. 假设您的数据表有四列,请创建一个包含4个字段的自定义类。 Loop through the data table and convert it in to an array of objects of the custom class type. 循环遍历数据表并将其转换为自定义类类型的对象数组。

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. 数据表无法直接序列化为JSON。 refer What's the best way to jSON serialize a .NET DataTable in WCF? 请参阅在WCF中jSON序列化.NET DataTable的最佳方法什么?

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}}];",
     ...

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

I created a gist when I put the solution. 当我提出解决方案时,我创造了一个要点

I hope this works for you... 我希望这对你有用......

Regards, 问候,

只需使用NewtonSoft Json并致电:

var JsonString = JsonConvert.SerializeObject(NameofDT, Formatting.Indented)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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