Export to Excel control derived from GridView
<p>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.</p>

<pre><code>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);
</code></pre>

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

<blockquote>
  <p>Uncaught Sys.WebForms.PageRequestManagerParserErrorException:
  Sys.WebForms.PageRequestManagerParserErrorException: The message
  received from the server could not be parsed.</p>
</blockquote>

<p>From my understanding, I think this is because I am using Response.Write in DownloadExcelFile(). However, I thought according to this <a href="http://weblogs.asp.net/leftslipper/archive/2007/02/26/sys-webforms-pagerequestmanagerparsererrorexception-what-it-is-and-how-to-avoid-it.aspx" rel="nofollow">link</a> that adding the PostBackTrigger would avoid this.  I have also replaced the PostBackTrigger generation in the InitializePager event with </p>

<pre><code>ScriptManager.GetCurrent(Page).RegisterPostBackControl(download);
</code></pre>

<p>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:</p>

<pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master"
    AutoEventWireup="true" CodeBehind="somePage.aspx.cs" Inherits="somePage" %&gt;
&lt;asp:Content ID="SomeContent" ContentPlaceHolderID="MainContent" runat="server"&gt;
    &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt;
        &lt;ContentTemplate&gt;
            Some other junk...
        &lt;/ContentTemplate&gt;
    &lt;/asp:UpdatePanel&gt;
    &lt;asp:UpdatePanel runat="server" ID="SomePanel" UpdateMode="Conditional"&gt;
        &lt;ContentTemplate&gt;
            &lt;asp:CustomGridView ID="SomeCustomGridView" runat="server" AutoGenerateColumns="False"
                DataSourceID="SomeSqlDataSource" AllowPaging="True" AllowSorting="True"&gt;
                &lt;Columns&gt;
                    &lt;asp:BoundField DataField="SomeField" /&gt;
                &lt;/Columns&gt;
            &lt;/asp:CustomGridView&gt;
            &lt;asp:SqlDataSource ID="SomeSqlDataSource" runat="server" ConnectionString="&lt;%$ ConnectionStrings:SomeConnectionString %&gt;"
                SelectCommand="&lt;%$ Resources: SomeResource, Query %&gt;"&gt;
                &lt;SelectParameters&gt;
                    &lt;asp:QueryStringParameter Name="SomeId" QueryStringField="SomeId" /&gt;
                &lt;/SelectParameters&gt;
            &lt;/asp:SqlDataSource&gt;
        &lt;/ContentTemplate&gt;
    &lt;/asp:UpdatePanel&gt;
&lt;/asp:Content&gt;
</code></pre>

<p>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.</p>
