我通过ObjectDataSource
中的GridView
在Sql数据库中显示一些数据。我还允许用户使用以下代码将数据导出到Excel:
public override void VerifyRenderingInServerForm(Control control)
{
}
private void ExportGridToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName = "Results" + DateTime.Now + ".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
GridView1.GridLines = GridLines.Both;
GridView1.HeaderStyle.Font.Bold = true;
GridView1.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();
}
protected void Button3_Click(object sender, EventArgs e)
{
ExportGridToExcel();
}
一切正常。但是,当我在AllowSorting=true
中设置GridView
并单击导出按钮时出现以下错误:ystem.InvalidOperationException:
只能在Render();
期间调用RegisterForEventValidation
这不是一个大问题,因为我不一定需要在GridView
中进行排序,但我很好奇这个问题是什么?
答案 0 :(得分:0)
将此属性添加到@ Page声明:
EnableEventValidation="false"