Export GridView Data to Excel. 从GridView导出数据到Excel的奇怪问题解析

时间:2022-03-12 11:48:39

 GridView导出函数内容如下  

 string attachment = "attachment; filename=Contacts.xls";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridView1.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();

并且重写了下面方法,

public override void VerifyRenderingInServerForm(Control control)
        {
        }

页面在点击Export按钮后任然没有反应。

本质原因在于GridView控件在Asp.Net的UpdatePanel中,解决方法就是把GridView移出到UpdatePanel之外。