我正在尝试在运行时将我的动态生成的数据表
加载到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 function或What's the best way to jSON serialize a .NET DataTable in WCF?有关此主题的更多信息。
关于c# - C# 动态加载数据表到 JS 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23832760/