0

Sorry if its too absurd question. but I have an asp button that calls an events. Event creates a datatable and downloading the datatable as xls. It works fine, I can see the downloaded file but I can not see the gridview that I show the datatable. Does anybody have an idea about it.

            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();

I tried create datatable and import to gridview after response.flush(). When I debugging the code below it works fine. I can see the variables in debug section but gridview still not seeable.

            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;
            }
Be Ta
  • 45
  • 11
  • I don't see any gridview code. Only `Reponse.Write` – Nick.Mc Feb 26 '19 at 10:35
  • this is the second event first one is like simply dt=connectdb(query); GridView1.DataSource = dt; GridView1.DataBind(); GridView1.Visible=true; – Be Ta Feb 26 '19 at 10:37
  • Why not post all the code in the first place? If you use the F12 console and inspect the document model, is there any sign of the gridview there? Does your query return anything? – Nick.Mc Feb 26 '19 at 10:44
  • Start using a specialized library for creating Excel files, like [EPPlus](https://github.com/JanKallman/EPPlus). [Example here](https://stackoverflow.com/a/47293207/5836671) and [here](https://stackoverflow.com/a/39513057/5836671). All you are doing now is creating a HTML page with an .xls extension. – VDWWD Feb 26 '19 at 10:46
  • yes it works well I otherwise the file would be empty. File shows the query results. and also i think its because of HttpContext.Current.Response.Flush(); this . when I commented out this line it shows the gridview but do not download file – Be Ta Feb 26 '19 at 10:46

1 Answers1

0

In your case you have to create a new .aspx page let say "download.aspx" where u will have code for downloading, and in your main page you can have your grid binding code, look the below example.

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();
  }

}
Neeraj Kumar Gupta
  • 2,157
  • 7
  • 30
  • 58