0

I have a control that is derived from the GridView class. The BottomPagerRow is always visible. Overriding InitializePager, I added an UpdatePanel and a LinkButton (Click event points to function to convert data) inside the UpdatePanel.

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);

When I click the LinkButton in a page using the control I see the following client side Exception in the web browser,

Uncaught Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

From my understanding, I think this is because I am using Response.Write in DownloadExcelFile(). However, I thought according to this link that adding the PostBackTrigger would avoid this. I have also replaced the PostBackTrigger generation in the InitializePager event with

ScriptManager.GetCurrent(Page).RegisterPostBackControl(download);

However, the result remains the same, some times it works and other times I see the exception. The exception is visible in a page similar to the following:

<%@ 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>

The master page that this child page uses includes a ToolkitScriptManager. I am using .NET Framework 4. If anyone has input on how to avoid this exception I would appreciate it. Not sure if I'm adding the trigger in the wrong part of the control's lifecycle, or just completely missed the point of the linked article. Thanks.

MoarGbs
  • 68
  • 5
  • 1
    maybe this is not direct answer to your question, but I find much easier to use EPPlus, here is the example of ASHX handler that will return *real* xlsx file, just set handler url to your link button and you are done : http://stackoverflow.com/a/9569827/351383 – Antonio Bakula Aug 17 '12 at 15:48
  • I modified my control to stored the GridView in a session parameter, then redirected the page to the ASHX handler and using EPPlus. This seems to avoid the Exception, but is more of a workaround. That said, the file is more user friendly. – MoarGbs Aug 20 '12 at 17:56

0 Answers0