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.