ASP.NET:将asp:Table值保存到csv

时间:2013-04-02 06:51:33

标签: javascript asp.net

我有一个图表可以在运行时在clickevent上添加数据点。我也使用javascript将数据点放在asp:table中。无论如何,我可以在服务器端获取表值。我尝试过EnableViewState但没有用。

代码:

protected void Page_Load(object sender, EventArgs e)
    {
        Data data = Utility.FromCSV();
        Highcharts chart = new Highcharts("chart")
           .InitChart(new Chart
           {
               DefaultSeriesType = ChartTypes.Scatter,
               Margin = new[] { 70, 50, 60, 80 },
               Events = new ChartEvents { Click = "ChartClickEvent" }
           })
           .AddJavascripFunction("ChartClickEvent", @"var x = Math.round(e.xAxis[0].value),
                            y = Math.round(e.yAxis[0].value),
                            series = this.series[0];
                            addNewData('Table1',x,y);
                            series.addPoint([x, y]);", "e")
           .SetTitle(new Title { Text = "User supplied data" })
           .SetSubtitle(new Subtitle { Text = "Click the plot area to add a point. Click a point to remove it." })
           .SetXAxis(new XAxis
           {
               MinPadding = 0.2,
               MaxPadding = 0.2
           })
           .SetYAxis(new YAxis
           {
               Title = new YAxisTitle { Text = "Value" },
               MinPadding = 0.2,
               MaxPadding = 0.2,
               PlotLines = new[]
                                                  {
                                                      new YAxisPlotLines
                                                      {
                                                          Value = 0,
                                                          Width = 1,
                                                          Color = ColorTranslator.FromHtml("#808080")
                                                      }
                                                  }
           })
           .SetLegend(new Legend { Enabled = false })
           .SetExporting(new Exporting { Enabled = false })
           .SetPlotOptions(new PlotOptions
           {
               Series = new PlotOptionsSeries
               {
                   LineWidth = 1,
                   Point = new PlotOptionsSeriesPoint
                   {
                       Events = new PlotOptionsSeriesPointEvents
                       {
                           Click = "function() { if (this.series.data.length > 1) this.remove(); }"
                       }
                   }
               }
           })
           .SetSeries(new Series { Data = data });
        for (int i = 0; i < data.DoubleArrayData.Length / 2; i++)
        {
            TableRow tr0 = Table1.Rows[0];
            TableCell myTableCell = new TableCell();
            myTableCell.Text = data.DoubleArrayData[i, 0].ToString();
            tr0.Cells.Add(myTableCell);
            TableRow tr1 = Table1.Rows[1];
            myTableCell = new TableCell();
            myTableCell.Text = data.DoubleArrayData[i, 1].ToString();
            tr1.Cells.Add(myTableCell);
        }
        string chartstring = chart.ToHtmlString();
        Literal lt = new Literal();
        lt.Text = chartstring;
        PlaceHolder1.Controls.Add(lt);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        String[,] data = new String[Table1.Rows[0].Cells.Count-1, 2];
        for(int i=1;i<Table1.Rows[0].Cells.Count;i++)
        {
            data[i-1, 0] = Table1.Rows[0].Cells[i].Text;
            data[i-1, 1] = Table1.Rows[1].Cells[i].Text;
        }
        var Writer = new StreamWriter(File.OpenWrite(@"D:\logs\dataout.csv"));
        for (int j = 0; j < data.Length / 2; j++)
            Writer.WriteLine(data[j, 0] + "," + data[j, 1]);
        Writer.Close();
    }

asp.net page:

enter image description here

0 个答案:

没有答案