只能在执行 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.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 ManageContents
    
Inherits System.Web.UI.Page

    
Protected Sub Page_Load(ByVal sender As ObjectByVal 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(ByVal sender As ObjectByVal 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(ByVal control As Control)

    
End Sub
End Class
分类:  ASP.NET


本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/archive/2012/05/16/2504290.html,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值