最近在用asp.net写网页遇到一个奇怪的问题,为了保证页面无刷新,使用了updatepanel控件,原来写好的导出excel的功能失效了,在网上搜了好久,发现有人总结了以下几种解决方法,我验证了第一种,也行的通,现分享出来,大家一起学习下

在ASP.NET中应用微软AJAX技术,把GridView在UpdatePanel导出Excel时会出现的错误。
就是在ASP.NET2.0框架中当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误提示。

方法一:(好像还是不行)
在ajax里面Response.Write会有冲突,你得为UpdatePanel添加 属性,把ControlID指向导出按扭的ID
导出Excel" OnClick="Button3_Click" />
   
   
       
   



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", "p_w_upload;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", "p_w_upload;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", "p_w_upload;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(不推荐)
2.直接在导出Execl的页面修改
<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true" CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>
3用ScriptManager.RegisterClientScriptBlock()等方法