六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 27|回复: 0

只能在执行 Render() 的过程中调用 RegisterForEventValidation;

[复制链接]

升级  96%

54

主题

54

主题

54

主题

秀才

Rank: 2

积分
194
 楼主| 发表于 2013-1-26 13:33:15 | 显示全部楼层 |阅读模式
当用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" %>

还要在后台代码重写
 public override void VerifyRenderingInServerForm(Control control)
        {
        }
导出代码
protected void btnExcel_Click(object sender, EventArgs e)
        {
            dvList.AllowPaging = false;
            dvList.Columns[0].Visible = false;
            UpdateView();
            Response.ClearContent();
            Response.Charset = "UTF8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("content-disposition", "attachment; filename= " + HttpUtility.UrlEncode("学生信息.xls", Encoding.UTF8).ToString());
            Response.ContentType = "application/excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            dvList.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表