当用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(); 
    }
}

当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误的更多相关文章

  1. C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation

    C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation 后台添加以下方法:/// <summa ...

  2. C# GridView 导出Excel表

    出错1:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内解决方案:在后台文件中重载VerifyRenderingInServerForm方法,如 ...

  3. gridview导出exe处理时遇到RegisterForEventValidation can only be called during Render();错误

    如题,今天在利用GridView作execl导出时,遇到RegisterForEventValidation can only be called during Render();问题.根据网上找到的 ...

  4. asp.net教程:GridView导出到Excel或Word文件

    asp.net教程:GridView导出到Excel或Word文件</ br> 在项目中我们经常会遇到要求将一些数据导出成Excel或者Word表格的情况,比如中国移动(我是中国移动用户) ...

  5. Export GridView Data to Excel. 从GridView导出数据到Excel的奇怪问题解析

    GridView导出函数内容如下 string attachment = "attachment; filename=Contacts.xls";            Respo ...

  6. Winform数据导出Execl小工具

    前台界面.cs文件 using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  7. asp.net中导出Execl的方法

    一.asp.net中导出Execl的方法: 在 asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址 输出在浏览器上:一种是将文件直接将文件输出流写给 ...

  8. Asp.net Gridview导出Excel

    前台页面放一个GridView什么的就不说了,要注意的是在 <%@ Page Language="C#" AutoEventWireup="true" C ...

  9. DevExpress GridControl GridView 导出到 Excel 类

    说明: 1>GridView 导出到 Excel (如果分页,只导出当前页数据) 2>GridView 导出到 Excel 3>方法2可以参考DataTable 导出到 Excel ...

随机推荐

  1. 《View Programming Guide for iOS》之frame、bounds和center之间的关系

    The frame property contains the frame rectangle, which specifies the size and location of the view i ...

  2. Java容器类概述

    1.简介 容器是一种在一个单元里处理一组复杂元素的对象.使用集合框架理论上能够减少编程工作量,提高程序的速度和质量,毕竟类库帮我们实现的集合在一定程度上时最优的.在Java中通过java.util为用 ...

  3. 多态-II(接口实现)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. PHP文件系统处理(二)

    1.文件的打开和关闭(读文件中的内容,向文件中写内容)            读取文件中的内容                file_get_contents()     //php5以上 < ...

  5. 【Zhejiang University PATest】02-1. Reversing Linked List

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  6. HDU 4869 (递推 组合数取模)

    Problem Turn the pokers (HDU 4869) 题目大意 有m张牌,全为正面朝上.进行n次操作,每次可以将任意ai张反面,询问n次操作可能的状态数. 解题分析 记正面朝上为1,朝 ...

  7. linux 网络联接

    安装完进入系统以后需要配置ip 方法一:图形界面直接配置,在system下的prefrecens下面的network下面设置 方法二: 在安装虚拟机的时候选择“bridge networking ”网 ...

  8. Azure SQL 数据库弹性池现已面市

    我们高兴地宣布Azure SQL 数据库弹性池现已正式面市.弹性池自去年试运行以来,得到许多软件即服务(SaaS)供应商和企业开发人员的认可,他们利用弹性池管理持续增长的云数据库和应用程序,成果高效. ...

  9. Core Java Volume I — 3.10. Arrays

    3.10. ArraysAn array is a data structure that stores a collection of values of the same type. You ac ...

  10. ZOJ Problem Set - 3329 One Person Game

    题目大意:有三个骰子,分别有k1,k2,k3个面. 每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和. 当分数大于n时结束.求游戏的期望步数.初始分数为0分析  设 E[i ...