title: asp.NET Drop Down List within a Grid View - Getting the selected value
body: <p>I have a status drop down list which I am using within a grid view, the users can select a list item they want when editing a row within the grid view. The problem is that the current implementation is not retrieving the selected value, but is only retrieving the default/loaded value.</p>
  
  <p>This is the defination of the grid view:</p>
  
  <pre><code>    &lt;asp:GridView ID="applicationGrid" runat="server"  Width="95%"
          AutoGenerateEditButton="True" 
          AutoGenerateColumns="False" 
          ShowFooter="True"
          CellSpacing="10" 
          HeaderStyle-HorizontalAlign="Left" 
          ItemStyle-HorizontalAlign="Left"
          OnRowUpdating="applicationGrid_RowUpdating" 
          OnRowEditing="applicationGrid_RowEditing" 
          OnRowCancelingEdit="applicationGrid_RowCancelingEdit"
          AutoPostBack="true" &gt;
  </code></pre>
  
  <p>And this is the defination of the Column where the dropdown list will appear on edit:</p>
  
  <pre><code>                &lt;asp:TemplateField HeaderText="Status"&gt;
                  &lt;ItemTemplate&gt;   
                      &lt;asp:Label ID="StatusDescription" runat="server" Text='&lt;%# Eval("STATUS_DESCRIPTION") %&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:DropDownList ID="StatusDescriptionList" runat="server" DataTextField="status_description"
                              DataValueField="application_status_code" OnLoad="DropDownLoadEdit"&gt;
                         &lt;asp:ListItem Text="Status:" Value="default"&gt;&lt;/asp:ListItem&gt;
                      &lt;/asp:DropDownList&gt;
                  &lt;/EditItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p>This is the code behind which is handling the update scenario:</p>
  
  <pre><code>    protected void applicationGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
          GridViewRow row = applicationGrid.Rows[e.RowIndex];
          applicationGrid.EditIndex = -1;
  
          Label applicationCodeLabel = row.FindControl("AppID") as Label;
          TextBox applicationNameTextBox = row.FindControl("AppNameEdit") as TextBox;
          TextBox applicationURLTextBox = row.FindControl("AppURLEdit") as TextBox;
          DropDownList applicationStatusDropDownList = row.FindControl("StatusDescriptionList") as DropDownList;
  
          int applicationCode = Convert.ToInt32(applicationCodeLabel.Text);
          string applicationName = applicationNameTextBox.Text;
          string applicationURL = applicationURLTextBox.Text;
          int applicationStatus = Convert.ToInt32(applicationStatusDropDownList.SelectedValue.ToString());
          //string applicationStatus2 = applicationStatusDropDownList.SelectedItem.Value;
          //string applicationStatus3 = applicationStatusDropDownList.SelectedItem.Text;
  
          application.UpdateApplication(applicationCode, applicationName, applicationURL);
          PopulateApplications();
      }
  </code></pre>
  
  <p>All is working, but the selected value is not the one which the is loaded and not the one which the user selects. Therefore the problem is getting the selected value from the list. What needs to be changed, and why?</p>
  
  <pre><code>        protected void applicationGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
          GridViewRow row = applicationGrid.Rows[e.RowIndex];
          applicationGrid.EditIndex = -1;
  
          Label applicationCodeLabel = row.FindControl("AppID") as Label;
          TextBox applicationNameTextBox = row.FindControl("AppNameEdit") as TextBox;
          TextBox applicationURLTextBox = row.FindControl("AppURLEdit") as TextBox;
          DropDownList applicationStatusDropDownList = row.FindControl("StatusDescriptionList") as DropDownList;
  
          int applicationCode = Convert.ToInt32(applicationCodeLabel.Text);
          string applicationName = applicationNameTextBox.Text;
          string applicationURL = applicationURLTextBox.Text;
          int applicationStatus = Convert.ToInt32(applicationStatusDropDownList.SelectedValue.ToString());
          //string applicationStatus2 = applicationStatusDropDownList.SelectedItem.Value;
          //string applicationStatus3 = applicationStatusDropDownList.SelectedItem.Text;
  
          application.UpdateApplication(applicationCode, applicationName, applicationURL);
          PopulateApplications();
      }
  </code></pre>
  
  <p>EDIT: Adding my Populate Methods:</p>
  
  <pre><code>        protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              try
              {
                  PopulateApplications();
              }
              catch (Exception exception)
              {
                  throw exception;
              }
          }
      }
  
      private void PopulateApplications()
      {
          DataTable reader = application.GetApplicationList();
          applicationGrid.DataSource = reader;
          applicationGrid.DataBind();
          applicationGrid.AllowSorting = true;
      }
  
      protected void DropDownLoadEdit(object sender, EventArgs e)
      {        
              DataTable statusTable = application.GetStatusList();
  
              DropDownList dropdown = sender as DropDownList;
              dropdown.DataSource = statusTable;
              dropdown.DataTextField = "status_description";
              dropdown.DataValueField = "application_status_code";
              dropdown.DataBind()
      }
  </code></pre>
  
  <p>Update #2: I am trying to fill up a static variable in the class which is for the selected index. This will then be used when update is pressed. However, this is still getting the original value of the drop down list and not the selected one. </p>
  
  <p>This is the method:</p>
  
  <pre><code>    protected void StatusDescriptionList_SelectedIndexChanged(object sender, System.EventArgs e)
      {
          DropDownList ddl = (DropDownList)sender;
          selectedValue = Convert.ToInt32(ddl.SelectedValue.ToString());
      }
  </code></pre>