title: Export Gridview Excel Not Working in ASP.NET
body: <p>I am using the Export Gridview to Excel. Before the Gridview appears, the user will do some filtering. I guess my Export button doesnt seem to work because I didnt put my Gridview on the page load....</p>
  
  <p>heres my export code:</p>
  
  <pre><code>   protected void Button2_Click(object sender, EventArgs e)
      {
          Response.ClearContent();
          Response.AppendHeader("content-disposition", "attachment; filename=logbook.xls");
          Response.ContentType = "application/excel";
  
          StringWriter stringWriter = new StringWriter();
          HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
  
          GridView1.RenderControl(htmlTextWriter);
          Response.Write(stringWriter.ToString());
          Response.End();
      }
  </code></pre>
  
  <p>my Gridview Code:</p>
  
  <pre><code>       protected void btnSearch_Click(object sender, EventArgs e)
      {
          BindGridviewData(ddlSystemList.SelectedValue);                    
      }
  
  
      private void BindGridviewData(string s)
      {
  
          string ConnectionStringB = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
          using (SqlConnection connectionB = new SqlConnection(ConnectionStringB))
          {
              connectionB.Open();
              SqlCommand cmd = new SqlCommand();
  
              if (s == "Webloan")
              { 
                  cmd.Connection = connectionB;
                  cmd.CommandText = "Select a.ldatetime as DATE, a.username as USERNAME, a.fullname as FULLNAME, a.description as DESCRIPTION, b.action_name as ACTION from WEBLOAN_SERVER.webloan.dbo.logbook a join WEBLOAN_SERVER.webloan.dbo.action_def b on a.action_no = b.action_no  where DATEDIFF(day,ldatetime,@date_exec1) &lt;= 0 and DATEDIFF(day,ldatetime,@date_exec2) &gt;= 0";
                  cmd.CommandType = CommandType.Text;
              }
  
              if (s == "Saveplus")
              {
                  cmd.Connection = connectionB;
                  cmd.CommandText = "Select top 10 from WEBLOAN_SERVER.saveplus.dbo.logbook where  DATEDIFF(day,ldatetime,@date_exec) = 0";
                  cmd.CommandType = CommandType.Text;
              }
  
              cmd.Parameters.AddWithValue("@date_exec1", txtDate.Text);
              cmd.Parameters.AddWithValue("@date_exec2", txtDate2.Text);
  
              SqlDataAdapter da = new SqlDataAdapter(cmd);
  
              DataSet ds = new DataSet();
  
  
  
              da.Fill(ds);
              GridView1.DataSource = ds;          
              GridView1.DataBind();
              connectionB.Close();
          }
  
      }
  
         protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
       {
          GridView1.PageIndex = e.NewPageIndex; //assign new page number
          BindGridviewData(ddlSystemList.SelectedValue);
          //GridView1.DataBind(); // bind data to gridview
       }
  </code></pre>
  
  <p>my page load code:</p>
  
  <pre><code>     protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {
  
              txtDate.Text = DateTime.Now.ToString("MM/d/yyyy");
              txtDate2.Text = DateTime.Now.ToString("MM/d/yyyy");     
  
          }     
  
       }
  </code></pre>
  
  <p>i also added my override code:</p>
  
  <pre><code>    public override void VerifyRenderingInServerForm(Control control)
      {
      }
  </code></pre>
  
  <p>Im new in ASP... I badly need help on this one.. Thank You</p>
  
  <p>PS: Im using Update Panel and my Generate to Excel Button is inside my Update Panel</p>