当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误
当用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;      using System.Data;
using System.Data;      using System.Configuration;
using System.Configuration;      using System.Collections;
using System.Collections;      using System.Web;
using System.Web;      using System.Web.Security;
using System.Web.Security;      using System.Web.UI;
using System.Web.UI;      using System.Web.UI.WebControls;
using System.Web.UI.WebControls;      using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls.WebParts;      using System.Web.UI.HtmlControls;
using System.Web.UI.HtmlControls;      using System.Data.SqlClient;
using System.Data.SqlClient;      using System.IO;
using System.IO;      
      public partial class EXportTest : System.Web.UI.Page
public partial class EXportTest : System.Web.UI.Page      {
{      protected void Page_Load(object sender, EventArgs e)
 protected void Page_Load(object sender, EventArgs e)      {
 {      if (!Page.IsPostBack)
 if (!Page.IsPostBack)      {
 {      BindData();
            BindData();   }
        }      
      }
    }      
      private string ConnectionString
 private string ConnectionString      {
 {      
      get { return @"Server=localhost;Database=Northwind;
 get { return @"Server=localhost;Database=Northwind;      Trusted_Connection=true"; }
        Trusted_Connection=true"; }      
      }
    }      
      
      
      private void BindData()
 private void BindData()      {
 {      // make the query
 // make the query   string query = "SELECT * FROM Categories";
 string query = "SELECT * FROM Categories";      SqlConnection myConnection = new SqlConnection(ConnectionString);
        SqlConnection myConnection = new SqlConnection(ConnectionString);      SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
        SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);      DataSet ds = new DataSet();
        DataSet ds = new DataSet();      ad.Fill(ds, "Categories");
        ad.Fill(ds, "Categories");      GridView1.DataSource = ds;
        GridView1.DataSource = ds;      GridView1.DataBind();
        GridView1.DataBind();   
      /* It's a good idea if you can Cache the DataSet */
 /* It's a good idea if you can Cache the DataSet */      
      }
    }      
      public override void VerifyRenderingInServerForm(Control control)
 public override void VerifyRenderingInServerForm(Control control)      {
 {      
      // Confirms that an HtmlForm control is rendered for
 // Confirms that an HtmlForm control is rendered for      the specified ASP.NET server control at run time.
        the specified ASP.NET server control at run time.      
      }
    }      
      protected void Button1_Click(object sender, EventArgs e)
 protected void Button1_Click(object sender, EventArgs e)      {
 {      Response.Clear();
        Response.Clear();      
      Response.AddHeader("content-disposition",
        Response.AddHeader("content-disposition",      "attachment;filename=FileName.xls");
 "attachment;filename=FileName.xls");      
      Response.Charset = "";
        Response.Charset = "";      
      // If you want the option to open the Excel file without saving than
 // If you want the option to open the Excel file without saving than      
      // comment out the line below
 // comment out the line below      
      // Response.Cache.SetCacheability(HttpCacheability.NoCache);
 // Response.Cache.SetCacheability(HttpCacheability.NoCache);      
      Response.ContentType = "application/vnd.xls";
        Response.ContentType = "application/vnd.xls";      
      System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();      
      System.Web.UI.HtmlTextWriter htmlWrite =
        System.Web.UI.HtmlTextWriter htmlWrite =      new HtmlTextWriter(stringWrite);
 new HtmlTextWriter(stringWrite);      
      // turn off paging
 // turn off paging   GridView1.AllowPaging = false;
        GridView1.AllowPaging = false;      BindData();
        BindData();   
      
      GridView1.RenderControl(htmlWrite);
        GridView1.RenderControl(htmlWrite);      
      Response.Write(stringWrite.ToString());
        Response.Write(stringWrite.ToString());      
      Response.End();
        Response.End();      
      // turn the paging on again
 // turn the paging on again   GridView1.AllowPaging = true;
        GridView1.AllowPaging = true;      BindData();
        BindData();      
      }
    }      
      protected void GridView1_PageIndexChanging(object sender,
 protected void GridView1_PageIndexChanging(object sender,      GridViewPageEventArgs e)
    GridViewPageEventArgs e)      {
 {      GridView1.PageIndex = e.NewPageIndex;
        GridView1.PageIndex = e.NewPageIndex;      BindData();
        BindData();   }
    }      }
}
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号

