1.在前台页面Page指令处添加 EnableEventValidation="false"
后台实现:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
// 此方法必须重写 用于生成html
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
// 导出
private void ToExcel(Control ctl, string FileName)
{
HttpContext.Current.Response.Charset = " UTF-8 " ;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = " application/ms-excel " ;
HttpContext.Current.Response.AppendHeader( " Content-Disposition " , " attachment;filename= " + "" + FileName);
ctl.Page.EnableViewState = false ;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
3.GridView的自动分页关闭后才可正确导出所有数据