Search code examples
c#asp.netgridviewupdatepanelexport-to-excel

Export a GridView in an update panel to Excel and then refresh the grid - not working


I have a gridview in an update panel. I can export it to an excel using HTTP response and it exports the required data and open the file in a pop up which is all working fine.

But, once the data is exported I have to update the grid view indicating that these data were exported. I verified the correct data is set to the data source and databind is called. But the excel export is not triggering the screen to refresh.

If I trigger a refresh by changing a drop down or by something else, I can see the data changed. I tried UpdatePanelID.Update() - still in vain.

So how to trigger a gridview refresh after the excel export?

Thanks in advance.

My code for export:

        var excelXml = GetExcelXml(dsInput, filename);
        response.Clear();
        response.AppendHeader("Content-Type", "application/vnd.ms-excel");
        response.AppendHeader("Content-disposition", "attachment; filename=" + filename);
        response.Write(excelXml);
        response.Flush();

protected void btnExport_Click(object sender, EventArgs e)
{
    try
    {

        if (list.Count() > 0)
        {


            ds.Tables.Add(dtForExport);
            ExcelHelper.ToExcel(ds, filename); //To Excel method is described above.
            LoadGridDetails();//Binds the new values

        }

    }
    catch (Exception ex)
    {
        lblStatus.Text = "Error Exporting to Excel";
    }
}//Screen is not refreshed after executing this line.

Solution

  • What you call a «Popup», it seems to me that you are talking about the browser download popup. So, in this case, it wont work, because you already did an Response.Clear and you can't anymore do an refresh of your grid.

    My suggestion is: you can open an real popup it means: window.popup, and this popup will do all the response job, or you can also put some java-script in your button "export" to do two things: do the post-back to export function and wait X milliseconds to call a click of some hidden button just to make a second post-back and do the refresh.

    Hope it helps