UpdatePanel中的GridView导出成Excel

             昨天,弄一个GridView的导出Excel,整死我了,这个GridView被封装成GridViewEx,在页面中使用时,还用了Updatepanel。说说一波三折吧

            首先,遇到这个问题:

类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内

解决方法
在 aspx.cs
里加
public override void VerifyRenderingInServerForm(Control control)
{
     //base.VerifyRenderingInServerForm(control);
}

然后,又遇到这个问题:
只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误提示

解决方法

1.修改web.config(不推荐)

< pages  enableEventValidation  ="false"   ></ pages >

2.直接在导出Execl的页面修改

<% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " ExportWordByIO.aspx.cs "  Inherits = " _Default "  EnableEventValidation  =   " false "    %>

最后,还遇到了这个问题:
Sys.WebForms.PageRequestManagerParserErrorException:
分析从服务器收到的消息,之所以出现此错误,常见的原因是:通过调用Response.Write()修改相应时,将启用响应筛选器、HttpModules或服务器追踪
解决方法如下:  

1.如果调用Response.Write()方法的服务器控件在使用UpdatePanel的页面,则只需要在UpdatePanel下增加一个<Triggers>节点,通过PostBackTrigger注册一下改控件就可以了。代码如下:  

  1. <asp:ScriptManager ID="ScriptManager1" runat="server">  
  2. </asp:ScriptManager>  
  3. <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
  4. <Triggers>  
  5. <asp:PostBackTrigger ControlID="Button2" /> <!--Button2就是下面那个需要在Button2_Click事件里使用Response.Write()的按钮ID-->  
  6. </Triggers>  
  7. <ContentTemplate>   
  8. <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />  
  9. <asp:UpdateProgress ID="UpdateProgress1" runat="server">  
  10. <ProgressTemplate></ProgressTemplate>  
  11. </asp:UpdateProgress>  
  12. </ContentTemplate>  
  13. </asp:UpdatePanel>  

终于搞好了,谢天谢地啊大笑

贴上导出方法吧
  public static void ExportGridViewExToExcel(Com.UI.Web.GridViewEx dgData, string excelFileName)
        {
            //如果文件名为中文,必须编码为UTF8

            excelFileName = System.Web.HttpUtility.UrlEncode(excelFileName, System.Text.Encoding.UTF8);

            System.Web.HttpContext curContext = System.Web.HttpContext.Current;

            System.IO.StringWriter strWriter = null;
            System.Web.UI.HtmlTextWriter htmlWriter = null;
            bool allowPaging, allowSorting;

            if (dgData != null)
            {
                curContext.Response.Clear();
                curContext.Response.Buffer = false;
                curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                //curContext.Response.ContentEncoding   =   System.Text.Encoding.GetEncoding("GB2312");

                curContext.Response.Charset = "";
                curContext.Response.AppendHeader("Content-Disposition", "online;filename=" + excelFileName + ".xls");
                curContext.Response.ContentType = "application/vnd.ms-excel";

              //  dgData.Page.EnableViewState = false;
                strWriter = new System.IO.StringWriter();
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
                //对于排序的GridViewEx,先要关掉分页和排序功能 
                allowPaging = dgData.AllowCustomerPaging; 
                allowSorting = dgData.AllowSorting;

                if (allowPaging)
                    dgData.AllowCustomerPaging = false;
                if (allowSorting)
                    dgData.AllowSorting = false;

                if (allowPaging || allowSorting )
                    dgData.BindData();

                dgData.RenderControl(htmlWriter);
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();

                if (allowPaging)
                    dgData.AllowCustomerPaging = allowPaging;
                if (allowSorting)
                    dgData.AllowSorting = allowSorting;
                if (allowPaging || allowSorting)
                    dgData.BindData();

            }
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值