Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack
<p>I have two buttons with onclick event handlers that process information from two grid views on the page. One button for each gv. These are situated on top of each other nested in a html table structure.</p>

<p>The buttons are used to export the grid date into an Excel document (see code below)</p>

<p>The top button and grid when click on the button works fine but the bottom button throws a <code>ThreadAbortException: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.</code></p>

<p>Naturally I've googled for this but some of the top results were dealing with <code>Response.Redirect()</code> calls rather than <code>Response.End()</code>. One such post at <a href="http://forums.asp.net/t/984756.aspx" rel="nofollow">Forums.asp.net</a> has the same error on the same method call but the solution was to changing the code <code>Response.Redirect()</code> with an error page as parameter - again not related to what I have.</p>

<p>Another <a href="http://support.microsoft.com/kb/312629/en-us" rel="nofollow">search at Microsoft Suport page</a> suggest a solution where <code>HttpContext.Current.ApplicationInstance.CompleteRequest()</code> replaces <code>Response.End()</code>. I've tried this, the error goes away and so is the Excel download popup.</p>

<p>So I don't know where to go from here. What's weird is that the same code (less gridview id) works for one but the other. Here is the code for your review and I've marked where the error is thrown. 
I thought perhaps I could spawn an new thread - would that alleviate the issue? I've never done a multi-threaded app but I'm up for a challenge. Please advise. Thanks!</p>

<pre><code>&lt;table&gt;
  &lt;tr&gt;
   &lt;td align="left"&gt;
     &lt;asp:Button ID="btnExport" runat="server" OnClick="btnExport_Click"
          Text="Export" Visible="false" /&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;                    
     &lt;asp:Panel runat="server" ID="pnl1" Visible="false"&gt;
      &lt;asp:GridView ID="gvCountTotalsCat" runat="server" 
          AutoGenerateColumns="false"
          CellPadding="3" PageSize="25" BackColor="White" BorderColor="MidnightBlue"
          BorderStyle="Groove" BorderWidth="1px" CssClass="TextCompact" 
          GridLines="Vertical"
          OnRowDataBound="gridView_OnRowDataBound"
          EmptyDataText="Your request has returned zero records"&gt;
          &lt;Columns&gt;
           &lt;asp:TemplateField&gt;
            &lt;HeaderTemplate&gt;
             &lt;asp:Label runat="server" ID="lblHeader" Text="Cat" /&gt;
            &lt;/HeaderTemplate&gt;
           &lt;ItemTemplate&gt;
            &lt;asp:Literal ID="litWuc" runat="server" /&gt;
           &lt;/ItemTemplate&gt;
           &lt;/asp:TemplateField&gt;
            &lt;asp:BoundField DataField="Cat Entries" HeaderText="Cat Entries" /&gt;
           &lt;asp:TemplateField&gt;
           &lt;HeaderTemplate&gt;
            &lt;asp:Label runat="server" ID="lblHeader" /&gt;
           &lt;/HeaderTemplate&gt;
           &lt;ItemTemplate&gt;
            &lt;asp:Literal ID="litSum" runat="server" /&gt;
           &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
         &lt;/Columns&gt;                                              
        &lt;/asp:GridView&gt;
       &lt;/asp:Panel&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td align="left"&gt;
    &lt;asp:Button ID="btnExport1" runat="server" OnClick="btnExport_Click1" 
         Text="Export" Visible="false" /&gt;
   &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
   &lt;td&gt;                         
    &lt;asp:Panel runat="server" ID="pnl2" Visible="false"&gt;
     &lt;asp:GridView ID="gvCountTotalsCat1" runat="server" 
          AutoGenerateColumns="false"
          AllowPaging="false" CellPadding="3" PageSize="25" BackColor="White"
          BorderColor="MidnightBlue" BorderStyle="Groove" BorderWidth="1px" 
          CssClass="TextCompact" GridLines="Vertical" 
          OnRowDataBound="gridView_OnRowDataBound"
          EmptyDataText="Your request has returned zero records"&gt;
          &lt;Columns&gt;
            &lt;asp:TemplateField&gt;
             &lt;HeaderTemplate&gt;
              &lt;asp:Label runat="server" ID="lblHeaderWuc" Text="Wuc" /&gt;
             &lt;/HeaderTemplate&gt;
            &lt;ItemTemplate&gt;
            &lt;asp:Literal ID="litWuc" runat="server" /&gt;
            &lt;/ItemTemplate&gt;
           &lt;/asp:TemplateField&gt;
           &lt;asp:BoundField DataField="Wuc Entries" HeaderText="Wuc Entries" /&gt;
           &lt;asp:TemplateField&gt;
            &lt;HeaderTemplate&gt;
              &lt;asp:Label runat="server" ID="lblHeader" /&gt;
            &lt;/HeaderTemplate&gt;
            &lt;ItemTemplate&gt;
              &lt;asp:Literal ID="litSum" runat="server" /&gt;
            &lt;/ItemTemplate&gt;
           &lt;/asp:TemplateField&gt;
          &lt;/Columns&gt;     
         &lt;/asp:GridView&gt;
        &lt;/asp:Panel&gt;
       &lt;/td&gt;
      &lt;/tr&gt;
     &lt;/table&gt;



public void btnExport_Click(object sender, System.EventArgs e)
{
    string attachment = string.Empty;
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    // Create a form to contain the grid
    HtmlForm frm = new HtmlForm();
    frm.Attributes["runat"] = "server";

    attachment = "attachment; filename=gvCountTotalsCat_" + _selectedSite + ".xls";
    gvCountTotalsCat.Parent.Controls.Add(frm);
    frm.Controls.Add(gvCountTotalsCat);

    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";

    frm.RenderControl(htw);

    Response.Write(sw.ToString());

    Response.End();
}



public void btnExport_Click1(object sender, System.EventArgs e)
{
    string attachment = string.Empty;
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    Response.Clear();
    Response.ClearHeaders();

    // Create a form to contain the grid
    HtmlForm frm = new HtmlForm();
    frm.Attributes["runat"] = "server";

    attachment = "attachment; filename=gvCountTotalsCat1_" + _selectedSite + ".xls";
    gvCountTotalsCat1.Parent.Controls.Add(frm);
    frm.Controls.Add(gvCountTotalsCat1);

    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";

    frm.RenderControl(htw);

    Response.Write(sw.ToString());

    try
    {
 &gt;&gt; Error thrown here &gt;&gt;     Response.End();

    }
    catch (System.Threading.ThreadAbortException lException)
    {
        lException;
    }
}
</code></pre>
