HttpContext取消标签中的文本

时间:2018-07-23 14:25:35

标签: c# asp.net

我在ASP.NET应用程序中有一个简单的btnPullDataIntoCsv_Click方法,该方法将内容打印到标签中,从SQL Server表中下载数据并按如下方式推入csv:

protected void btnPullDataIntoCsv_Click(object sender, EventArgs e)
    {
        lblUserMessage.Text = String.Empty;
        PullData p = new PullData();

        // Print something into label
        lblUserMessage.Text = "I'm printing text into label.";
        lblUserMessage.ForeColor = System.Drawing.Color.Blue;
        lblUserMessage.Font.Bold = true;

        // pull the data from sql table into csv
        p.pullDataIntoCsv(start, stop);
    }
PullData类中的

pullDataIntoCsv方法只是从SQL Server表中下载数据并将数据推送到csv中。

public void pullDataIntoCsv()
        {
            StringBuilder sb = new StringBuilder();
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlDataAdapter da = new SqlDataAdapter(@"select * from someTable", con);
                da.Fill(dt);

                string[] columnNames = dt.Columns.Cast<DataColumn>().
                                                  Select(column => column.ColumnName).
                                                  ToArray();
                sb.AppendLine(string.Join(",", columnNames));

                foreach (DataRow row in dt.Rows)
                {
                    string[] fields = row.ItemArray.Select(field => field.ToString()).
                                                    ToArray();
                    sb.AppendLine(string.Join(",", fields));
                }

                HttpContext context = HttpContext.Current;

                context.Response.Write(sb.ToString());
                context.Response.ContentType = "text/csv";
                context.Response.AddHeader("Content-Disposition", "attachment; filename=someData.csv");
                context.Response.End();
            }
        }

不知道为什么,但是当我触发btnPullDataIntoCsv_Click方法时,我看不到标签中的文本。我对此很陌生,所以请您提示我我做错了什么或我的代码中缺少什么?

0 个答案:

没有答案