标题:将GridView数据导出到excel文件遇到错误:只能在执行 Render() 的过程中调用 ...
取消只看楼主
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
结帖率:100%
 问题点数:0 回复次数:1 
将GridView数据导出到excel文件遇到错误:只能在执行 Render() 的过程中调用 Regis

以下的代码在运行时出现错误:只能在执行 Render() 的过程中调用 RegisterForEventValidation ,如何解决啊?

protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=lessons.xls");
Response.Charset = "gb2312";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());
Response.End();

GridView1.AllowPaging = true;
GridView1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control) { }


“/showbaobiao”应用程序中的服务器错误。
--------------------------------------------------------------------------------

只能在执行 Render() 的过程中调用 RegisterForEventValidation;
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: 只能在执行 Render() 的过程中调用 RegisterForEventValidation;

源错误:


行 104: GridView1.AllowPaging = false;
行 105: GridView1.DataBind();
行 106: GridView1.RenderControl(htmlWrite);
行 107:
行 108: Response.Write(stringWrite.ToString());

源文件: d:\myweb\showbaobiao\Default.aspx.cs 行: 106

堆栈跟踪:


[InvalidOperationException: 只能在执行 Render() 的过程中调用 RegisterForEventValidation;]
System.Web.UI.ClientScriptManager.RegisterForEventValidation(String uniqueId, String argument) +2083444
System.Web.UI.ClientScriptManager.GetPostBackEventReference(PostBackOptions options, Boolean registerForEventValidation) +49
System.Web.UI.WebControls.LinkButton.AddAttributesToRender(HtmlTextWriter writer) +228
System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer) +17
System.Web.UI.WebControls.DataControlLinkButton.Render(HtmlTextWriter writer) +34
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +130
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +24
System.Web.UI.WebControls.TableCell.RenderContents(HtmlTextWriter writer) +97
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +29
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +130
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +24
System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer) +7
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +29
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
System.Web.UI.WebControls.Table.RenderContents(HtmlTextWriter writer) +605
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +29
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +130
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +24
System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer) +7
System.Web.UI.WebControls.GridView.Render(HtmlTextWriter writer, Boolean renderPanel) +202
System.Web.UI.WebControls.GridView.Render(HtmlTextWriter writer) +30
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
_Default.Button1_Click(Object sender, EventArgs e) in d:\myweb\showbaobiao\Default.aspx.cs:106
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102


搜索更多相关主题的帖子: Regis 过程中 Render excel GridView 
2007-03-09 16:08
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
得分:0 

当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误提示。
有两种方法可以解决以上问题:
1.修改web.config(不推荐)<pages enableEventValidation ="false" ></pages>
2.直接在导出Execl的页面修改
<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true"

CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>

附:Gridview导出Execl可分页全部导出代码(C#)


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class EXportTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}

}

private string ConnectionString
{

get { return @"Server=localhost;Database=Northwind;
Trusted_Connection=true"; }

}

private void BindData()
{
// make the query
string query = "SELECT * FROM Categories";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "Categories");
GridView1.DataSource = ds;
GridView1.DataBind();

/**//* It's a good idea if you can Cache the DataSet */

}

public override void VerifyRenderingInServerForm(Control control)
{

// Confirms that an HtmlForm control is rendered for
the specified ASP.NET server control at run time.

}

protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();

Response.AddHeader("content-disposition",
"attachment;filename=FileName.xls");

Response.Charset = "";

// If you want the option to open the Excel file without saving than

// comment out the line below

// Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/vnd.xls";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);

// turn off paging
GridView1.AllowPaging = false;
BindData();

GridView1.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

// turn the paging on again
GridView1.AllowPaging = true;
BindData();

}

protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
}VB.net:(GridView直接帮定DataSource控件)
Imports Microsoft.VisualBasic
Imports System
Imports System.IO


Partial Class ManageContentsClass ManageContents
Inherits System.Web.UI.Page

Protected Sub Page_Load()Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'If Not Me.IsPostBack Then
' If Request.QueryString("username") <> "xucanzhao" Then
' Response.Redirect("default.aspx?returnUrl=nousername")
' End If
'End If
' ClientScript.RegisterStartupScript(Me.GetType(), "haha", "<script>alert('haha');</script>")
End Sub

Protected Sub btnExportExcel_Click()Sub btnExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportExcel.Click
Response.Clear()
Response.Buffer = True

Response.Charset = "utf-8"

Response.AddHeader("Content-Disposition", "attachment;filename=FileFlow.xls")
Response.ContentType = "application/ms-excel"
Dim strWriter As New StringWriter()
Dim htw As New HtmlTextWriter(strWriter)
GridView1.AllowPaging = False
GridView1.DataBind()
GridView1.RenderControl(htw)
Response.Write(strWriter.ToString)
Response.End()
GridView1.AllowPaging = True
GridView1.DataBind()
End Sub
Public Overrides Sub verifyRenderingInServerForm()Sub verifyRenderingInServerForm(ByVal control As Control)

End Sub
End Class


我是2.0超级菜鸟,请多多教导!
2007-03-09 16:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-123386-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.062404 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved