C# 导出到从GridView派生的Excel控件
我有一个从GridView类派生的控件。底部页面箭头始终可见。覆盖InitializePage,我在UpdatePanel中添加了一个UpdatePanel和一个LinkButton(单击事件点以转换数据)C# 导出到从GridView派生的Excel控件,c#,asp.net,C#,Asp.net,我有一个从GridView类派生的控件。底部页面箭头始终可见。覆盖InitializePage,我在UpdatePanel中添加了一个UpdatePanel和一个LinkButton(单击事件点以转换数据) var download = new LinkButton { Text = "Open in Excel", CssClass = "fakeButton", ID = "DownloadToExcel" }; download.Click += DownloadExcelFile; va
var download = new LinkButton { Text = "Open in Excel", CssClass = "fakeButton", ID = "DownloadToExcel" };
download.Click += DownloadExcelFile;
var up = new UpdatePanel() { ID = "ExcelUpdatePanel" };
var trigger = new PostBackTrigger { ControlID = download.ID };
up.Triggers.Add(trigger);
up.ContentTemplateContainer.Controls.Add(download);
row.Cells[0].Controls.Add(up);
当我在使用控件的页面中单击LinkButton时,我在web浏览器中看到以下客户端异常:
未捕获Sys.WebForms.PageRequestManagerParserErrorException:
Sys.WebForms.PageRequestManagerParserErrorException:消息
无法分析从服务器接收的数据
据我所知,我认为这是因为我使用的是Response.Write-in-DownloadExcelFile()。然而,根据这一点,我认为添加PostBackTrigger可以避免这种情况。我还将InitializePage事件中的PostBackTrigger生成替换为
ScriptManager.GetCurrent(Page).RegisterPostBackControl(download);
然而,结果仍然是一样的,有些时候它是有效的,有些时候我看到了异常。该异常在类似于以下内容的页面中可见:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master"
AutoEventWireup="true" CodeBehind="somePage.aspx.cs" Inherits="somePage" %>
<asp:Content ID="SomeContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Some other junk...
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" ID="SomePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:CustomGridView ID="SomeCustomGridView" runat="server" AutoGenerateColumns="False"
DataSourceID="SomeSqlDataSource" AllowPaging="True" AllowSorting="True">
<Columns>
<asp:BoundField DataField="SomeField" />
</Columns>
</asp:CustomGridView>
<asp:SqlDataSource ID="SomeSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:SomeConnectionString %>"
SelectCommand="<%$ Resources: SomeResource, Query %>">
<SelectParameters>
<asp:QueryStringParameter Name="SomeId" QueryStringField="SomeId" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
其他一些垃圾。。。
此子页使用的母版页包括ToolkitScriptManager。我使用的是.NETFramework4。如果有人对如何避免这种例外有意见,我将不胜感激。我不确定是在控件生命周期的错误部分添加触发器,还是完全没有抓住链接文章的重点。谢谢。也许这不是对您问题的直接回答,但我发现使用EPPlus要容易得多,下面是ASHX处理程序的示例,它将返回真实的xlsx文件,只需将处理程序url设置到链接按钮,您就完成了:我修改了控件,将GridView存储在会话参数中,然后使用EPPlus将页面重定向到ASHX处理程序。这似乎避免了例外情况,但更多的是一种变通方法。这就是说,该文件更加用户友好。