title: C# getting selected value from dropdownlist in gridview asp net
body: <p>How can I change the value of a textbox whenever a dropdownlist within a gridview has its value changed?</p>
  
  <p>On page load, the textbox shows the selected value, but when I change the selection of the dropdownlist, the textbox value doesn't change.</p>
  
  <p>The code is below.</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"&gt;
      &lt;Columns&gt;
          &lt;asp:TemplateField HeaderText="Entry"&gt;
              &lt;ItemTemplate&gt;
                  &lt;%# Container.DataItemIndex + 1 %&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="Duty"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:DropDownList ID="duty" runat="server" OnLoad = "ddl1_load" OnSelectedIndexChanged="duty_SelectedIndexChanged" autopostback="true" EnableViewState="true"&gt;&lt;/asp:DropDownList&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>The code behind is below.</p>
  
  <pre><code>protected void ddl1_load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
          DataTable dt = new DataTable();
          Duty dy = new Duty();
          dt = dy.getdutyid(Convert.ToInt32(dropcontractid.SelectedValue));
          DropDownList ddl = (DropDownList)sender;
          ddl.DataSource = dt;
          ddl.DataTextField = "dutyid";
          ddl.DataValueField = "dutyid";
          ddl.DataBind();
          TextBox1.Text = ddl.SelectedValue;
      }
  }
  </code></pre>