GridView导入至EXCEL (报错处理:只能在执行 Render() 的过程中调用 RegisterForEventValidation)
2012-05-16 11:39
381 查看
//注:在.aspx文件中第一行<%%>中加入EnableEventValidation="false"
方案二:
方案二
在实现"将GridView中的数据导出到Excel中"的时候出现了如下错误:
用户[b]代码未处理 InvalidOperationException
只能在执行 Render() 的过程中调用 RegisterForEventValidation;[/b]
EnableEventValidation属性是 .NET Framework 2.0 中是新增的属性,默认的情况下该属性的值为true;通过这个新增的功能ASP.NET会检查 POST方法中的所带的参数,如果认为不合法,就会抛出异常。这个设计的目的是为了防止恶意用户利用post 方法发送一些恶意数据,但是有时也会出现类似上面的错误。
只要禁止这个功能,问题就能得到解决。可以通过以下两种途径解决:
1、在Web.Config文件中:在<system.web></system.web>标记中添加如下代码:
<system.web>
<pages enableEventValidation="false"> </pages>
</system.web>
2、在具体的.aspx页面的源代码中修改代码,如下:
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="GridView_Export_Excel.aspx.cs" Inherits="GridView_Export_Excel" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CardSelect.aspx.cs" Inherits="PersonMag" EnableEventValidation="false" %> //这里一定要引入命名空间 using System.Data.SqlClient; //为导入Excel引入命名空间 using System.Drawing; using System.IO; using System.Text; private void GridViewBind() { //GridView的绑定 } private void Export(string FileType, string FileName) { //GridView导出到Excel,导出所有页 Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8;//换成UTF7编码出现乱码 Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); this.GridView1.AllowPaging = false;//取消分页 this.GridView1.AllowSorting = false;//取消排序 //this.GridView1.AutoGenerateColumns = true;//取消自动生成列 this.GridViewBind();//GridView的独立绑定函数 //让一列不显示的方法 //this.GridView1.Columns[0].Visible = false; GridView1.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); } protected void btnSqlToExcel_Click(object sender, EventArgs e) { //SqlToExcel按钮响应的事件 Export("application/ms-excel", "信息表.xls");//第一个参数是导出的类型,第二个参数是导出表的名称 } public override void VerifyRenderingInServerForm(Control control) { //重写此方法,确保程序运行时,指定的GridView控件总是位于<form runat="server">标记内 //base.VerifyRenderingInServerForm(control); }
方案二:
方案二
public void CreateExcel(DataSet ds, string FileName) { StringBuilder sb = new StringBuilder(); StringBuilder sbb = new StringBuilder(); HttpResponse resp; resp = Page.Response; resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); resp.ContentType = "application/ms-excel"; resp.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls"); this.EnableViewState = false; string colHeaders = "", Is_item = ""; int i = 0; //定义表对象与行对象,同时使用DataSet对其值进行初始化 DataTable dt = ds.Tables[0]; DataRow[] myRow = dt.Select(""); //取得数据表各列标题,标题之间以\t分割,最后一个列标题后加回车符 colHeaders += "<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">"; colHeaders += "<tr style="\" mce_style="\""font-weight: bold; white-space: nowrap;\">"; for (i = 0; i < dt.Columns.Count; i++) { colHeaders += "<td>" + dt.Columns[i].Caption.ToString() + "</td>"; } colHeaders += "</tr>"; resp.Write(colHeaders); //逐行处理数据 foreach (DataRow row in myRow) { Is_item += "<tr>"; //在当前行中,逐列取得数据,数据之间以\t分割,结束时加回车符\n for (i = 0; i < dt.Columns.Count; i++) { if (dt.Columns[i].ColumnName.Equals("银行帐号") || dt.Columns[i].ColumnName.Equals("身份证号")) Is_item += "<td style="\" mce_style="\""vnd.ms-excel.numberformat:@\">" + row[i].ToString() + "</td>"; else Is_item += "<td>" + row[i].ToString() + "</td>"; } Is_item += "</tr>"; resp.Write(Is_item); Is_item = ""; } resp.Write("</table>"); //写缓冲区中的数据到HTTP头文件中 resp.End(); }
在实现"将GridView中的数据导出到Excel中"的时候出现了如下错误:
用户[b]代码未处理 InvalidOperationException
只能在执行 Render() 的过程中调用 RegisterForEventValidation;[/b]
EnableEventValidation属性是 .NET Framework 2.0 中是新增的属性,默认的情况下该属性的值为true;通过这个新增的功能ASP.NET会检查 POST方法中的所带的参数,如果认为不合法,就会抛出异常。这个设计的目的是为了防止恶意用户利用post 方法发送一些恶意数据,但是有时也会出现类似上面的错误。
只要禁止这个功能,问题就能得到解决。可以通过以下两种途径解决:
1、在Web.Config文件中:在<system.web></system.web>标记中添加如下代码:
<system.web>
<pages enableEventValidation="false"> </pages>
</system.web>
2、在具体的.aspx页面的源代码中修改代码,如下:
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="GridView_Export_Excel.aspx.cs" Inherits="GridView_Export_Excel" %>
相关文章推荐
- 当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误提示
- 关于 c#导出 excel 遇到只能在执行 Render() 的过程中调用 RegisterForEventValidation的问题
- 解决导出excel中出现的错误:只能在执行 Render() 的过程中调用 RegisterForEventValidation
- GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用RegisterForEventValidation的错误提示
- 当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误
- 当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误
- 只能在执行Render() 的过程中调用 RegisterForEventValidation;
- 只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called during Render();
- 只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called during Render(); )
- 只能在执行 Render() 的过程中调用 RegisterForEventValidation;
- Error 之 只能在执行Render() 的过程中调用 RegisterForEventValidation;
- 只能在执行 Render() 的过程中调用 RegisterForEventValidation(RegisterForEventValidation can only be called during Render(); )
- 错误: Sys.WebForms.PageRequestManagerServerErrorException: 只能在执行 Render() 的过程中调用 RegisterForEventValidation;
- "只能在执行Render()的过程中调用RegisterForEventValidation" 解决方案
- 只能在执行 Render() 的过程中调用 RegisterForEventValidation
- 只能在执行Render() 的过程中调用 RegisterForEventValidation
- 只能在执行Render() 的过程中调用 RegisterForEventValidation
- 异常“只能在执行Render()的过程中调用RegisterForEventValidation”的解决办法(转)
- 执行Render()方法时出现 "只能在执行 Render() 的过程中调用 RegisterForEventValidation;"错误
- 只能在执行 Render() 的过程中调用 RegisterForEventValidation