ASP.NET的GridView在UpdatePanel里导出Excel的语句
在ASP.NET中应用微软AJAX技术,把GridView在UpdatePanel里导出Excel时会出现的错误。
就是在ASP.NET2.0框架中当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误提示。
方法一:(好像还是不行)
在ajax里面Response.Write会有冲突,你得为UpdatePanel添加 <triggers> 属性,把ControlID指向导出按扭的ID
<asp:Button ID="Button3" runat="server" Text="导出Excel" OnClick="Button3_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Button3"></asp:PostBackTrigger>
</Triggers>
CS:
protected void Button3_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "产品信息表.xls");
}
private void Export(string FileType, string FileName)
{
GridView1.EnableViewState = false;
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
方法二:(可以)
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
}
//必须重载此函数
public override void VerifyRenderingInServerForm(Control control)
{ }
方法四:(可能出现乱码)
public void CreateExcel(DataSet ds, string typeid, string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
string colHeaders = "", ls_item = "";
int i = 0;
//
DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select("");
// typeid=="1"為EXCEL文檔;typeid=="2"XML文檔
if (typeid == "1")
{
//
for (i = 0; i < dt.Columns.Count; i++)
{
if (i == dt.Columns.Count - 1)
{
colHeaders += dt.Columns[i].Caption.ToString() + "\n";
}
else
{
colHeaders += dt.Columns[i].Caption.ToString() + "\t";
}
}
//
resp.Write(colHeaders);
//
foreach (DataRow row in myRow)
{
//
for (i = 0; i < dt.Columns.Count; i++)
{
if (i == dt.Columns.Count - 1)
{
ls_item += row[i].ToString() + "\n";
}
else
{
ls_item += row[i].ToString() + "\t";
}
}
//
resp.Write(ls_item);
ls_item = "";
}
}
else
{
if (typeid == "2")
{
//从DataSet中直接导出XML数据并且写到HTTP输出流中
resp.Write(ds.GetXml());
}
}
//写缓冲区中的数据到HTTP头文件中
resp.End();
}
调用CreateExcel(ds, "1", "filename.xls");
还有三种方法可以解决以上问题:
1.修改web.config(不推荐)<pages enableEventValidation ="false" ></pages>
2.直接在导出Execl的页面修改
<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true" CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>
3用ScriptManager.RegisterClientScriptBlock()等方法
就是在ASP.NET2.0框架中当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误提示。
方法一:(好像还是不行)
在ajax里面Response.Write会有冲突,你得为UpdatePanel添加 <triggers> 属性,把ControlID指向导出按扭的ID
<asp:Button ID="Button3" runat="server" Text="导出Excel" OnClick="Button3_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Button3"></asp:PostBackTrigger>
</Triggers>
CS:
protected void Button3_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "产品信息表.xls");
}
private void Export(string FileType, string FileName)
{
GridView1.EnableViewState = false;
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
方法二:(可以)
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
}
//必须重载此函数
public override void VerifyRenderingInServerForm(Control control)
{ }
方法四:(可能出现乱码)
public void CreateExcel(DataSet ds, string typeid, string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
string colHeaders = "", ls_item = "";
int i = 0;
//
DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select("");
// typeid=="1"為EXCEL文檔;typeid=="2"XML文檔
if (typeid == "1")
{
//
for (i = 0; i < dt.Columns.Count; i++)
{
if (i == dt.Columns.Count - 1)
{
colHeaders += dt.Columns[i].Caption.ToString() + "\n";
}
else
{
colHeaders += dt.Columns[i].Caption.ToString() + "\t";
}
}
//
resp.Write(colHeaders);
//
foreach (DataRow row in myRow)
{
//
for (i = 0; i < dt.Columns.Count; i++)
{
if (i == dt.Columns.Count - 1)
{
ls_item += row[i].ToString() + "\n";
}
else
{
ls_item += row[i].ToString() + "\t";
}
}
//
resp.Write(ls_item);
ls_item = "";
}
}
else
{
if (typeid == "2")
{
//从DataSet中直接导出XML数据并且写到HTTP输出流中
resp.Write(ds.GetXml());
}
}
//写缓冲区中的数据到HTTP头文件中
resp.End();
}
调用CreateExcel(ds, "1", "filename.xls");
还有三种方法可以解决以上问题:
1.修改web.config(不推荐)<pages enableEventValidation ="false" ></pages>
2.直接在导出Execl的页面修改
<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true" CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>
3用ScriptManager.RegisterClientScriptBlock()等方法
posted on 2008-04-08 19:46 黄兆张's Blog 阅读(2870) 评论(3) 编辑 收藏 举报