GridView导出Excel
当用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" %>
<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true" CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

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

protected void ButtonExcel_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();

}

public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control)
// Confirms that an HtmlForm control is rendered for;
}

private void BindData()
{
string connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
string sql = "select * from products";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cm = new SqlCommand(sql, conn);
conn.Open();
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = cm;
DataTable tb = new DataTable();
ad.Fill(tb);
conn.Close();
this.GridView1.DataSource = tb;
this.GridView1.DataBind();
}

protected void GridView1_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
}
}
有两种方法可以解决以上问题:
1.修改web.config(不推荐)<pages enableEventValidation ="false" ></pages>
2.直接在导出Execl的页面修改
<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true" CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>
<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true" CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace NetSamples.aspnet
{
public partial class ExportGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void ButtonExcel_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();
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control)
// Confirms that an HtmlForm control is rendered for;
}
private void BindData()
{
string connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
string sql = "select * from products";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cm = new SqlCommand(sql, conn);
conn.Open();
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = cm;
DataTable tb = new DataTable();
ad.Fill(tb);
conn.Close();
this.GridView1.DataSource = tb;
this.GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
}
}
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利
This posting is provided "AS IS" with no warranties, and confers no rights.
This posting is provided "AS IS" with no warranties, and confers no rights.


浙公网安备 33010602011771号