title: How to bind gridview at pageload with dropdown list
body: <p>I have a dropdown list to search by categories.
  I need help to bind my gridview at page load, but at the same time, I also have a select command as votes.
  I know that there are codes such as Databinding in the pageload event. But for my case, i need to link the select command to a button to update votes. If i databind it, i could not grab the data key names to update my votes counter.
  Is there any way to bind the gridview, without removing the DataSourceID in the gridview itself?</p>
  
  <p>My aspx codes are as follow.</p>
  
  <pre><code>&lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                          ConnectionString="&lt;%$ ConnectionStrings:ConnectionString %&gt;" 
                          SelectCommand="SELECT * FROM [Review] WHERE ([Category] = @Category)"&gt;
                          &lt;SelectParameters&gt;
                              &lt;asp:ControlParameter ControlID="ddlCat" Name="Category" 
                                  PropertyName="SelectedValue" Type="String" /&gt;
                          &lt;/SelectParameters&gt;
                      &lt;/asp:SqlDataSource&gt;
  
  
  
                        &lt;asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                          ConnectionString="&lt;%$ ConnectionStrings:ConnectionString %&gt;" 
                          SelectCommand="SELECT [Category] FROM [ReviewCategory]"&gt;
                          &lt;/asp:SqlDataSource&gt;
  
  
                      &lt;asp:DropDownList ID="ddlCat" runat="server" 
                          DataSourceID="SqlDataSource2" DataTextField="Category" 
                          DataValueField="Category" AutoPostBack="True" 
                          onselectedindexchanged="SelectionChange"&gt;
                      &lt;/asp:DropDownList&gt;
  
  
  &lt;asp:GridView ID="GridView1" runat="server" Width="1114px" 
      Height="272px" AutoGenerateColumns="False" PageSize="5" 
          DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="ReviewID"&gt;
  &lt;Columns&gt;
  
      &lt;asp:BoundField DataField="Votes" HeaderText="Votes" 
          SortExpression="Votes" /&gt;
      &lt;asp:BoundField DataField="Category" HeaderText="Category" 
          SortExpression="Category" /&gt;
  
      &lt;asp:CommandField SelectText="VOTE as your FAVOURITE!" 
          ShowSelectButton="True" /&gt;
  &lt;/Columns&gt;
  </code></pre>
  
  <p></p>
  
  <p></p>
  
  <p><strong>c# code</strong></p>
  
  <pre><code> protected void btnVote_Click(object sender, EventArgs e)
  {
      int reviewid = Convert.ToInt16(GridView1.SelectedDataKey.Value);
  
      SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
      string sqlstmt = "select Votes from Review where ReviewID = '" + reviewid + "'";
      SqlCommand comm = new SqlCommand(sqlstmt, conn);
  
  
      try
      {
  
          conn.Open();
          SqlDataReader read = comm.ExecuteReader();
  
  
          if (read.Read())
          {
              int votes = (int)read["Votes"];
              votes += 1;
              string updatestatement = "Update Review set Votes= '" + votes + "' Where ReviewID = '" + reviewid + "'";
              SqlCommand command = new SqlCommand(updatestatement, conn);
             read.Close();
              command.ExecuteNonQuery();
          }
  
  
      }
      finally { 
          conn.Close();
          GridView1.DataBind();
      }
  
  }
  
       protected void SelectionChange(object sender, EventArgs e)
    {
  
      int stored = ddlCat.SelectedIndex;
  
      if (stored == 0)
      {
          SqlDataSource1.SelectCommand = "SELECT * from Review ORDER BY [Votes] DESC  ";
  
  
      }
      else { } 
  }
  </code></pre>