c# - 将数据表从 C# 动态传递到 Javascript

标签 c# javascript web-services wcf

aspx.cs文件

数据表是在运行时通过使用 C# 从 Excel 文件读取值来生成的。如何将其传递给javascript?

protected void btnRead_Click(object sender, EventArgs e)
{
      Upload();  //Upload File Method
      DataTable dt = ReadExcelWithStream(currFilePath); 
    //on button click it reads file path of excel and stores it in a string path 
    //calls the ReadExcelWithStream Method to read the excel File
}

  private void Upload()
        {
            HttpPostedFile file = this.fileSelect.PostedFile;
            string fileName = file.FileName;
            string tempPath = System.IO.Path.GetTempPath();   //Get Temporary File Path
            fileName = System.IO.Path.GetFileName(fileName); //Get File Name (not including path)
            this.currFileExtension = System.IO.Path.GetExtension(fileName);   //Get File Extension
            this.currFilePath = tempPath + fileName; //Get File Path after Uploading and Record to Former Declared Global Variable
            file.SaveAs(this.currFilePath);  //Upload
        }
    private DataTable ReadExcelWithStream(string path)
    {
       bool isDtHasColumn = false;   //Mark if DataTable Generates Column
       StreamReader reader = new StreamReader(path, System.Text.Encoding.Default);  //Data Stream
       while (!reader.EndOfStream)
       {
       string message = reader.ReadLine();
       string[] splitResult = message.Split(new char[] { ',' }, StringSplitOptions.None);  //Read One Row and Separate by Comma, Save to Array
       DataRow row = dt.NewRow();
       for (int i = 0; i < splitResult.Length; i++)
            {
                if (!isDtHasColumn) //If not Generate Column
                {
                    dt.Columns.Add("column" + i, typeof(string));
                }
                row[i] = splitResult[i];
            }
       dt.Rows.Add(row); 
       isDtHasColumn = true;  //Mark the Existed Column after Read the First Row, Not Generate Column after Reading Later Rows
        }
        //method(here coding to read the excel file and stores it in a dt is written)
        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

最佳答案

您不能简单地将对象传递给 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# 动态传递到 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23854620/

相关文章:

web-services - 支持延迟加载所有属性的 Web 服务模式

.net - 使用包含丹麦字符的 json 数据调用时出现 WCF Web 服务请求错误

java - 从服务发送数据到远程套接字时出现问题

javascript - jquery:检查当前切换的类

c# - 拟合 Akima 样条曲线

php - 建议制作在线编码竞赛网站的更好方法

javascript - 在定义后修改自定义元素类

multithreading - ReSTLet客户端句柄Web服务关闭

c# - 在 setter 中更改 SelectedItem 值对 ComboBox 没有影响

c# - 用于检测文本电子邮件的正则表达式