gridview导入到excel中的问题
我在按钮中的程序如下:
麻烦各位帮忙,谢谢
protected void Button3_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Write( " <meta http-equiv=Content-Type content=text/html;charset=gb2312> ");
Response.AppendHeader( "content-disposition ", "attachment;filename=\ " " + System.Web.HttpUtility.UrlEncode( "中文文件名 ", System.Text.Encoding.UTF8) + ".xls\ " ");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);//这儿出错,提示“只能在执行 Render() 的过程中调用 RegisterForEventValidation;”这是什么问题,该怎么解决,谢谢
Response.Write(sw.ToString());
Response.End();
}
只能在执行 Render() 的过程中调用 RegisterForEventValidation;
------解决方案--------------------在页面上加不验证属性: <%@ Page Language= "C# " EnableEventValidation = "false " AutoEventWireup= "true " CodeFile= "Default.aspx.cs " Inherits= "_Default " %>
------解决方案--------------------在StringWriter sw = new StringWriter();
前加一句:
Response.ContentType = "application/ms-excel ";
试下。
------解决方案--------------------你要确定GridView中是不是还有一些其他的控件,比如; DropDownList什么的