title: Grab SelectedValue of Dropdownlist in Gridview
body: <p>Creating a list of users that haven't updated their job title in a Gridview. I want the list to have a dropdown filled with all the possible title selections and a button next to the dropdown. Then a person can come in and change the title in the dropdown hit the button and its updated and removed from the list.</p>
  
  <p>I have all of this the way I want it to look but I'm trying to figure out how to pass the SelectedValue of the dropdown box in that row to the code behind OnClick. As you can see below the closest I can get is pass the row number in the CommandArgument. Any suggestions how I can get the SelectedValue of the dropdown of that specific row to the OnClick?</p>
  
  <p><strong>EDIT</strong>: Maybe I should be using OnRowCommand instead of OnClick?</p>
  
  <p>Looks like this currently:</p>
  
  <pre><code>John Doe | DropdownList  Button
  Jane Doe | DropdownList  Button
  Joe Doe | DropdownList  Button
  Jeff Doe |  DropdownList  Button
  </code></pre>
  
  <p>ASPX</p>
  
  <pre><code>&lt;asp:GridView runat="server" ID="TitleView" OnRowDataBound="TitleView_RowDataBound" AutoGenerateColumns="False"&gt;
                  &lt;Columns&gt;
                      &lt;asp:BoundField DataField="Fullname" HeaderText="Fullname" /&gt;
                      &lt;asp:TemplateField&gt;
                          &lt;ItemTemplate&gt;
                              &lt;div class="input-append"&gt;&lt;asp:DropDownList CssClass="span5" ID="TitleList" runat="server"&gt;
                              &lt;/asp:DropDownList&gt;
                                  &lt;asp:Button ID="lbnView" runat="server" Text="Update" CssClass="btn btn-primary" OnClick="btn_Clicked" 
                                              CommandArgument='&lt;%# ((GridViewRow)Container).RowIndex %&gt;'&gt;&lt;/asp:Button&gt;&lt;/div&gt;
                          &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                  &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Code Behind</p>
  
  <pre><code>public void bindTitleView()
      {
          using (SqlConnection conn = new SqlConnection(""))
          {
              SqlCommand cmd = new SqlCommand(@"SELECT U.First + ' ' + U.Last as Fullname, U.UserID, T.Name FROM Employees U LEFT JOIN Titles T ON U.Title = T.ID WHERE U.Active = '1' AND U.Title = '92' ORDER BY Fullname ASC", conn);
              conn.Open();
              SqlDataAdapter adp = new SqlDataAdapter(cmd);
              DataSet myDataSet = new DataSet();
              adp.Fill(myDataSet);
              TitleView.DataSource = myDataSet;
              TitleView.DataBind();
          }
      }
      protected void TitleView_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              DropDownList ddl = (DropDownList)e.Row.FindControl("TitleList");
              using (SqlConnection conn = new SqlConnection(""))
              {
                  SqlCommand cmd = new SqlCommand(@"SELECT ID, Name FROM Titles ORDER BY Name ASC", conn);
                  conn.Open();
                  SqlDataAdapter adp = new SqlDataAdapter(cmd);
                  DataTable myDataSet = new DataTable();
                  adp.Fill(myDataSet);
                  ddl.DataSource = myDataSet;
                  ddl.DataTextField = "Name";
                  ddl.DataValueField = "ID";
                  ddl.DataBind();
              }
          }
      }
      protected void btn_Clicked(object sender, EventArgs e)
      {
                  String rowid = ((Button)sender).CommandArgument;
      }
  </code></pre>
  
  <p><strong>SOLUTION:</strong> The answer I approved below worked for me once I added !IsPostBack to the Page_Load</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              bindTitleView();
          }
  </code></pre>