我想将数据从gridview导出到execl但是有execption [英] i want to export data from gridview to execl but there is execption
本文介绍了我想将数据从gridview导出到execl但是有execption的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将数据从gridview导出到execl但是
有execptionRegisterForEventValidation只能在Render()期间调用;
如何解决这个问题?????
i want to export data from gridview to execl but
there is execption "RegisterForEventValidation can only be called during Render();"
how to slove this?????
protected void Button3_Click(object sender, EventArgs e)
{
ExportGridView2Excel(GridView1, "test.xls");
}
private void ExportGridView2Excel(GridView gridViewControl, string excelFileName)
{
if (string.IsNullOrEmpty(excelFileName))
throw new Exception("excelFileName is missing");
if (!excelFileName.EndsWith(".xls"))
excelFileName += ".xls";
gridViewControl.AllowPaging = false;
gridViewControl.AllowSorting = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader(
"content-disposition",
"attachment;filename=" + excelFileName);
gridViewControl.RenderControl(hw); // execption in this line
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
推荐答案
添加
EnableEventValidation="false"
。
Just添加
EnableEventValidation="false"
to你的页面指令。
如果你搜索了CP,你可以在这个Q& A中结束: RegisterForEventValidation只能在Render()期间调用; [ ^ ]
祝你好运,
OI
to your page directive.
And if you searched CP you could have ended in this Q&A : RegisterForEventValidation can only be called during Render();[^]
Good luck,
OI
这篇关于我想将数据从gridview导出到execl但是有execption的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文