如何C#的DataTable传递给javascript函数 [英] how to pass c# datatable to a javascript function

查看:452
本文介绍了如何C#的DataTable传递给javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在thhe codebehind这些数据,并试图将其传递到各种格式的javascript函数:列表中,JSON字符串,但没有办法通过一个JavaScript变种对象获取的数据数组。
这里的数据在code后面的最后格式为:

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:

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

    }

然后试图用这种方法来调用javascriot:

Then tried to call javascriot in this way:

   string createArrayScript = string.Format("var array2 = [{0},{1},{2},{3}];",         string.Join(",",",",",", array2));

但返回一个错误:FormatException是由用户code未处理
索引(基于零)必须大于主题的列表的大小大于或等于零且小于

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.

这是调用该函数:
    Page.ClientScript.RegisterStartupScript(this.GetType(),registerPoiArray,createArrayScript,真实);

This is the call to the function: Page.ClientScript.RegisterStartupScript(this.GetType(), "registerPoiArray", createArrayScript, true);

下面的JavaScript VAR格式为:

Here the javascript var format:

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

我不能通过该数组中的JavaScript。请帮助我

I can not pass this array in javascript. Help me please

推荐答案

您可以创建一个自定义类来重新present的数据表。说,如果你的数据表有四列,创建一个具有4个字段的自定义类。遍历数据表,并将其转换到自定义的类的对象的数组。

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.

最后,你可以用下面的code序列化数组转换成JSON。

Finally you can serialize the array into json using the following code.

JavaScriptSerializer js = new JavaScriptSerializer();
js.Serialize(data);

,其中数据是对象数组

where data is the array of objects.

这是我使用的技巧。

数据表不能轻易直接序列化到JSON。参照<一个href=\"http://stackoverflow.com/questions/17928513/whats-the-best-way-to-json-serialize-a-net-datatable-in-wcf\">What's到JSON的最佳方式序列化.NET的DataTable在WCF?

Data table cannot be serialized easily to JSON directly. refer What's the best way to jSON serialize a .NET DataTable in WCF?

这篇关于如何C#的DataTable传递给javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆