title: Export to Excel stopped working when I added an AJAX panel
body: <p>I am just starting to learn how to use Ajax and I am stuck on this.  </p>
  
  <p>I have buttons in my ASP.net web site that work perfectly but the export to Excel and export to CSV stopped working after I added an Ajax panel.</p>
  
  <p>They worked before I added the panel and when I debug the function <code>To_Excel_Click</code> is called is processed but it just does not create the Excel file. Something in Ajax I think is preventing the file to be created.</p>
  
  <p>Thank you for any help </p>
  
  <p>HTML Ajax</p>
  
  <pre><code>&lt;asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"&gt;
      &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"&gt;
      &lt;contenttemplate&gt;
  &lt;asp:Button ID="BT1" runat="server" Text="Submit" Visible="false" onclick="BT1"/&gt;
   &lt;asp:Button ID="BT2" runat="server" Text="Submit" Visible="false" onclick="BT2" /&gt;
   &lt;asp:Button ID="BTMakeCVS" runat="server" Text="Submit to CVS" Visible="false" onclick="BTMakeCVS_Click" /&gt;
      &lt;asp:Button ID="To_Excel" runat="server" Text="To Excel" onclick="To_Excel_Click" Visible="false" AutoPostBack="True"/&gt;
      &lt;/fieldset&gt;&lt;fieldset class="Matainancefieldset3"&gt;
  &lt;asp:GridView ID="GVData" runat="server" Visible="false"&gt;
           &lt;RowStyle CssClass="Row" /&gt;
      &lt;AlternatingRowStyle CssClass="AltRow" /&gt; 
      &lt;PagerTemplate&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;
          &lt;asp:PlaceHolder ID="ph" runat="server"&gt;
          &lt;/asp:PlaceHolder&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
      &lt;/PagerTemplate&gt;
      &lt;/asp:GridView&gt;
   &lt;asp:Label ID="LBExcell" runat="server" Text="Model:" Visible="false"&gt;&lt;/asp:Label&gt;
   &lt;/contenttemplate&gt;
          &lt;/asp:UpdatePanel&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>C#</p>
  
  <pre><code>protected void To_Excel_Click(object sender, EventArgs e)
  {
          ExcelExport.Export("Summary.xls", GVData);
  }
  
  public class ExcelExport
  {
      public static void Export(string fileName, GridView gv)
      {
          HttpContext.Current.Response.Clear();
          HttpContext.Current.Response.AddHeader(
                "content-disposition", string.Format("attachment;filename={0}", fileName));
          HttpContext.Current.Response.ContentType = "application/ms-excel";
  
          using (StringWriter sw = new StringWriter())
          {
              using (HtmlTextWriter htw = new HtmlTextWriter(sw))
              {
                  Table table = new Table();
                  table.GridLines = gv.GridLines;
  
                  if (gv.HeaderRow != null)
                  {
                      ExcelExport.PrepareControlForExport(gv.HeaderRow);
                      table.Rows.Add(gv.HeaderRow);
                  }
  
                  foreach (GridViewRow row in gv.Rows)
                  {
                      ExcelExport.PrepareControlForExport(row);
                      table.Rows.Add(row);
                  }
  
                  if (gv.FooterRow != null)
                  {
                      ExcelExport.PrepareControlForExport(gv.FooterRow);
                      table.Rows.Add(gv.FooterRow);
                  }
  
                  table.RenderControl(htw);
  
                  HttpContext.Current.Response.Write(sw.ToString());
                  HttpContext.Current.Response.End();
              }
          }
      }
  
      private static void PrepareControlForExport(Control control)
      {
          for (int i = 0; i &lt; control.Controls.Count; i++)
          {
              Control current = control.Controls[i];
  
              if (current is LinkButton)
              {
                  control.Controls.Remove(current);
                  control.Controls.AddAt(i, new LiteralControl((current
  as LinkButton).Text));
              }
              else if (current is ImageButton)
              {
                  control.Controls.Remove(current);
                  control.Controls.AddAt(i, new LiteralControl((current
  as ImageButton).AlternateText));
              }
              else if (current is HyperLink)
              {
                  control.Controls.Remove(current);
                  control.Controls.AddAt(i, new LiteralControl((current
  as HyperLink).Text));
              }
              else if (current is DropDownList)
              {
                  control.Controls.Remove(current);
                  control.Controls.AddAt(i, new LiteralControl((current
  as DropDownList).SelectedItem.Text));
              }
              else if (current is CheckBox)
              {
                  control.Controls.Remove(current);
                  control.Controls.AddAt(i, new LiteralControl((current
  as CheckBox).Checked ? "True" : "False"));
              }
  
              if (current.HasControls())
              {
                  ExcelExport.PrepareControlForExport(current);
              }
          }
      }
  }
  </code></pre>