title: Export to Excel exports the whole webpage and not just the gridview(Sorting enabled)
body: <p>In my C# application, I am generating a Gridview from user's input and then I have to give user an option to export the Gridview to the excel sheet.</p>
  
  <p>Here is my abc.aspx page:</p>
  
  <pre><code>    &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;div&gt;
              &lt;b&gt;Enter p1 :&lt;/b&gt;
              &lt;asp:TextBox ID="tb_P1" runat="server" /&gt;
              &lt;br /&gt;
  
              &lt;b&gt;Enter p2 :&lt;/b&gt;
              &lt;asp:TextBox ID="tb_P2" runat="server" /&gt;&lt;br /&gt;
  
  
  
              &lt;asp:Button ID="btn1" runat="server" OnClick="Button1_Click" Text="Start Search" ClientIDMode="Static" /&gt;
             &lt;asp:Button ID="btn2" runat="server" OnClick="Button2_Click" Text="Export Data to Excel" /&gt;
              &lt;hr /&gt;
  
                      &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
  
                          ShowFooter="false"
                         AllowSorting="true"
                          OnSorting="GridView1_Sorting"
                          EnableViewState="false"
                         ShowHeaderWhenEmpty="True"
                          AllowPaging="false"&gt;
                          &lt;RowStyle Wrap="false" /&gt;
                          &lt;HeaderStyle Wrap="false" /&gt;
                      &lt;/asp:GridView&gt;    
          &lt;/div&gt;
  
      &lt;/form&gt;
      &lt;br /&gt;  
  &lt;/body&gt;
  </code></pre>
  
  <p>Here my .cs page:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.SqlClient;
  using System.Data;
  using System.Web.Script.Services;
  using System.Configuration;
  using System.Drawing;
  using System.Windows.Forms;
  using System.ComponentModel;
  using System.IO;
  
  public partial class pSearch : System.Web.UI.Page
  {
      SqlConnection sqlconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
      DataSet dsldata;
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
      protected void Button2_Click(object sender, EventArgs e)
      {
  
          string fname = filename.Text;
  
  
          GridView1.DataSource = (DataSet)Session["data"];
          GridView1.DataBind();
          int rowCount = GridView1.Rows.Count;
          if (rowCount == 0)
          {
              Response.Write("&lt;script&gt;alert('Result Empty!');&lt;/script&gt;");
          }
          else
          {
                  ExportToExcel(GridView1, fname);
          }
      }
  
  
  
      private void ExportToExcel(GridView GrdView, string fname)
      {
  
          try
          {
              Response.AddHeader("contentdisposition", "attachment;filename=test1.xls");
              Response.Charset = "";
              Response.ContentType = "application/vnd.xls";
              System.IO.StringWriter stringWrite = new System.IO.StringWriter();
              System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
              GrdView.RenderControl(htmlWrite);
              Response.Write(stringWrite.ToString());
              Response.End();
          }
          catch (Exception ex)
          {
              Response.Write("&lt;script&gt;alert('" + ex.Message + "')&lt;/script&gt;");
          }
      }
      public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
      {
      }
  
  
      protected void Button1_Click(object sender, EventArgs e)
      {
  
          string Rname= Page.Request.QueryString["rname"];
          string typeofquery = "my command";
  
          string abc = null;
         abc = "" + typeofquery + " @RName='" + RName "'";
  
          SqlDataAdapter cmdldata = new SqlDataAdapter(abc, sqlconn);
          cmdldata.SelectCommand.CommandTimeout = 600;
          dsldata = new DataSet();
  
          try
          {
              cmdldata.Fill(dsldata);
              Session["data"] = dsldata;
              GridView1.DataSource = dsldata;
              GridView1.DataBind();
          }//end of try
          catch (Exception ex)
          {
  
              Response.Write(ex);
  
          }//end of catch
  
      }
  
      private const string ASCENDING = " ASC";
      private const string DESCENDING = " DESC";
  
  
      public SortDirection GridViewSortDirection
      {
          get
          {
              if (ViewState["sortDirection"] == null)
                  ViewState["sortDirection"] = SortDirection.Ascending;
  
              return (SortDirection)ViewState["sortDirection"];
          }
          set { ViewState["sortDirection"] = value; }
      }
  
  
      protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
      {
          string sortExpression = e.SortExpression;
  
          if (GridViewSortDirection == SortDirection.Ascending)
          {
              GridViewSortDirection = SortDirection.Descending;
              SortGridView(sortExpression, DESCENDING);
          }
          else
          {
              GridViewSortDirection = SortDirection.Ascending;
              SortGridView(sortExpression, ASCENDING);
          }
  
      }
  
      private void SortGridView(string sortExpression, string direction)
      {
          dsldata = (DataSet)HttpContext.Current.Session["data"];
          DataTable dt = dsldata.Tables[0];
          DataView dv = new DataView(dt);
          dv.Sort = sortExpression + direction;
          dt = dv.ToTable();
          DataSet ds1 = new DataSet("table");
          ds1.Tables.Add(dt);
          Session["data"] = ds1;
          GridView1.DataSource = dv;
          GridView1.DataBind();
      }
  
  }
  </code></pre>
  
  <p>I have sorting enabled in my gridview.
  The problem is: my whole web page is getting exported to the excel sheet. I want only my grid view to be exported.The sorting is working properly and I do get new sorted table in my excel sheet but it is with the whole web page.
  I have looked for solution on different places on internet.Seems like many people have faced this problem.I have also tried their solutions like 
  changing the   Response.ContentType = "application/vnd.xls"; to<br>
  Response.ContentType = "application/vnd.ms-excel"; and most of the other solutions were similar to my  ExportToExcel() function.</p>
  
  <p>I have been wokring on this thing since last 2 days but nothing is working for my application.</p>
  
  <p>Please Help!
  Thank you in advance!</p>
  
  <p>Changed my ExporttoExcel function</p>
  
  <pre><code>private void ExportToExcel(GridView GrdView, string fname)
  {
  
  try
  {
  
      Response.Clear();
      Response.AddHeader("content-disposition", "attachment;filename=" + fname + ".xls");
      Response.Charset = "";
      Response.ContentType = "application/vnd.ms-excel";
      StringWriter stringWrite = new StringWriter();
      HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
      GridView1.RenderControl(htmlWrite);
      Response.Write(stringWrite.ToString());
      Response.End();
  }
  catch (Exception ex)
  {
      Response.Write("&lt;script&gt;alert('" + ex.Message + "')&lt;/script&gt;");
  }
  }
  </code></pre>