在Response.Flush()代码不起作用之后

时间:2019-02-26 10:29:31

标签: c# asp.net httpresponse

很抱歉,如果它太荒谬了。但我有一个ASP按钮来调用事件。事件创建一个数据表并将该数据表下载为xls。它工作正常,我可以看到下载的文件,但是看不到显示数据表的gridview。有人有想法吗?

            Response.ClearContent();
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254");
            Response.Charset = "windows-1254"; 
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/xsl";
            string tab = "";
            foreach (DataColumn c in dt.Columns)
            {
                Response.Write(tab + c.ColumnName);
                tab = "\t";
            }

            \\..somethings about writing to file ...

            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = true; 
            HttpContext.Current.ApplicationInstance.CompleteRequest();

我尝试创建datatable并在response.flush()之后导入到gridview中。当我调试下面的代码时,它可以正常工作。我可以在调试部分看到变量,但仍无法看到gridview。

            Response.ClearContent();
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254");
            Response.Charset = "windows-1254"; 
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/xsl";
            string tab = "";
            foreach (DataColumn c in dt.Columns)
            {
                Response.Write(tab + c.ColumnName);
                tab = "\t";
            }

            \\..somethings about writing to file ...

            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = true; 
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            queryString = textbox.Text.Replace("\r\n", " ").ToString();
            table = connectDatabase(queryString);
            GridView1.DataSource = table;
            GridView1.DataBind();
            if (GridView1.Rows.Count > 0)
            {
                GridView1.Visible = true;
            }

1 个答案:

答案 0 :(得分:0)

在您的情况下,您必须创建一个新的.aspx页面,说“ download.aspx”,您将在其中具有要下载的代码,在您的主页中,您可以拥有网格绑定代码,请看下面的示例。

MainPage.aspx

btnGetData_click()
{
 dt=connectdb(query); 
 GridView1.DataSource = dt; 
 GridView1.DataBind(); 
 GridView1.Visible=true;

 Session("ExcelData") = dt;

 string url = "download.aspx";
 string s = "window.open('" + url + "', 'download_window', 'width=100,height=100,left=100,top=100,resizable=no');";
 ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);

}

download.aspx

page_load()
{
  if(!IsPostBack)
  {
            DataTable dt = (DataTable)Session("ExcelData");
            Response.ClearContent();
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254");
            Response.Charset = "windows-1254"; 
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/xsl";
            string tab = "";
            foreach (DataColumn c in dt.Columns)
            {
                Response.Write(tab + c.ColumnName);
                tab = "\t";
            }

            \\..somethings about writing to file ...

            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = true; 
            HttpContext.Current.ApplicationInstance.CompleteRequest();
  }

}