c# - C# 动态加载数据表到 JS 数组

标签 c# javascript web-services datatable

我正在尝试在运行时将我的动态生成的数据表加载到JavaScript中的JS数组中。但无法通过webservice传递,因为只能通过javascript调用静态方法。如何将数据表返回到javascript中。

aspx.cs 文件:

protected void btnRead_Click(object sender, EventArgs e)
{
//on button click it reads file path of excel and stores it in a string path 
}

private DataTable ReadExcelWithStream(string path)

{
//method reads the excel file and stores it in a dt
ForJs(dt);
     return dt;
 }
[ScriptMethod, WebMethod]
 public static DataTable ForJs(DataTable dt)
 {

     return dt;
 }

aspx 文件:

 <script type="text/javascript">
        function InsertLabelData() {
           PageMethods.ForJs(onSuccess, onFailure);

        }

             function onSuccess(dt) {

    //attach the table to dhtmlx grid
    }

但是数据表没有传递给 JavaScript。 如何将数据表从 C# 传递到 javascript? 或任何其他将数据表从 C# 传递到 JS 的方法

请帮忙。

谢谢!

最佳答案

duplicate?/related question: Dynamically pass datatable from C# to Javascript

您不能简单地将对象传递给 JavaScript。它必须采用可用的格式。类似于数组或序列化为 XML 或 JSON。还有“DataTables contain lots of additional information which JSON cannot store”。

看看how to pass c# datatable to a javascript functionWhat's the best way to jSON serialize a .NET DataTable in WCF?有关此主题的更多信息。

关于c# - C# 动态加载数据表到 JS 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23832760/

相关文章:

c# - ThreadAbort异常

javascript - 隐藏模态并使用 CSS3 转换显示它

c# - 处理 C# WebService 异常

ios - 从服务中获取值

java - 网络托管上的多处理

c# - 如何将 JSON 属性值转换为键和值数组

c# - 在 Application.Run() 之后更新表单

javascript - 使用 jQuery 检测滚动方向的变化

javascript - 一页上有多个选项卡部分

c# - 将 shared_ptr 与托管语言引用进行比较