Locked Export GridView to Excel in web part

Locked

  • Wednesday, December 10, 2008 11:27 AM
     
      Has Code
    Hello
    I was trying to export grid view to excel,  in pure asp.net it works fine. But in Sharepoint web part after I click Export to Excel link button, other controls stop working and work only after manual page refresh. How to make it work in sharepoint?

    //export to excel click handler  
    protected void BtnExportGrid_Click(object sender, EventArgs args)  
    {  
            HttpContext.Current.Response.Clear();  
            HttpContext.Current.Response.AddHeader(  
                "content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));  
            HttpContext.Current.Response.ContentType = "application/ms-excel";  
     
            using (StringWriter sw = new StringWriter())  
            {  
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))  
                {  
                    //  Create a table to contain the grid  
                    Table table = new Table();  
     
                    //  add each of the data rows to the table  
                    foreach (GridViewRow row in gv.Rows)  
                    {  
                        GridViewExportUtil.PrepareControlForExport(row);  
                        table.Rows.Add(row);  
                    }  
     
                    //  render the table into the htmlwriter  
                    table.RenderControl(htw);  
     
                    //  render the htmlwriter into the response  
                    HttpContext.Current.Response.Write(sw.ToString());  
                    HttpContext.Current.Response.End();  
                }  
            }  
        }  
     

Answers