【问题标题】:Dynamically pass datatable from C# to Javascript将数据表从 C# 动态传递到 Javascript
【发布时间】:2014-07-14 07:29:30
【问题描述】:

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

【问题讨论】:

  • ReadExcelWithStream 是如何被调用的?而ForJs 需要一个DataTable,从哪里来?
  • 我在 btnRead_Click 上调用了 ReadExcelWithStream 方法。并且数据表是在 ReadExcelWithStream 方法中生成的,我没有提到它可能会占用大量空间。 @christiandev
  • 您提供的信息越多(在合理范围内),其他人帮助您的机会就越大。这条信息是这里的关键。
  • 请立即评论@christiandev

标签: c# javascript web-services wcf


【解决方案1】:

您不能简单地将 (y) 对象传递给 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? 了解有关此主题的更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    相关资源
    最近更新 更多