title: ASP.NET Textbox text does not change while trying to update
body: <p>I have a form where there is a ListBox, a textbox and four buttons called <code>Save</code>, <code>Edit</code>, <code>Delete</code> and <code>Clear</code>. I'm retrieving data from database and populating the ListBox. When I select one of the items in the ListBox, it is populated into the textbox. Now when I delete that item, there is nothing wrong, it works fine. But when I try to update that item, i.e. change the text in the textbox and then click the <code>Edit</code> button, there is problem. In the textbox, I can see the text is being changed, no problem with that, but when I debugged, I found in the backend, the textbox is still containing the old text and not the modified one. </p>
  
  <p>What am I doing wrong?</p>
  
  <p>Here's my UI code:</p>
  
  <pre><code>&lt;asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server"&gt;
      &lt;h1&gt;Employee Category&lt;/h1&gt;
      &lt;table&gt;
          &lt;td&gt;
              &lt;asp:ListBox ID="listEmployeeCategory" runat="server" Height="164px" Width="210px" AutoPostBack="true" /&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="lblMessage" runat="server" Text="" Visible="false" /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;
                          &lt;asp:Label ID="lblEmpCategoryName" runat="server" Text="Name: " Font-Bold="true" /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="txtEmpCategoryName" runat="server" /&gt;
                      &lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:RequiredFieldValidator ID="rqdEmpCategoryName" ControlToValidate="txtEmpCategoryName" ErrorMessage="Employee Category Name can't be empty!" Style="color:Red" runat="server" /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
              &lt;asp:Button ID="btnSave" Text="Save" runat="server" OnClick="btnSave_Click" /&gt;
              &lt;asp:Button ID="btnEdit" Text="Edit" runat="server" OnClick="btnEdit_Click" /&gt;
              &lt;asp:Button ID="btnDelete" Text="Delete" runat="server" OnClick="btnDelete_Click" /&gt;
              &lt;asp:Button ID="btnCancel" Text="Cancel" runat="server" /&gt;
          &lt;/td&gt;
      &lt;/table&gt;
  &lt;/asp:Content&gt; 
  </code></pre>
  
  <p>And here's my backend code, for the sake of everyone's understanding, I'm posting my entire backend code (except the BL, DAL and DAO codes):</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;
  using Shelter.DAO.MasterEntry;
  using Shelter.BLL.MasterEntry;
  
  namespace Shelter.UI.MasterEntry
  {
      public partial class EmployeeCategoryUI : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              if (!IsPostBack)
              {
                  LoadDataIntoListBox();
              }
  
              else
              {
                  if (listEmployeeCategory.SelectedIndex != -1)
                  {
                      LoadDataIntoTextBox(Convert.ToInt32(listEmployeeCategory.SelectedValue));  
                  }
              }
          }
  
          private void LoadDataIntoTextBox(int val)
          {
              EmployeeCategory objEmployeeCategory = new EmployeeCategory();
              EmployeeCategoryBLL empCategoryBLL = new EmployeeCategoryBLL();
              objEmployeeCategory.ID = Convert.ToInt32(val);
              DataTable EmpCategoryDt = new DataTable();
              EmpCategoryDt = empCategoryBLL.RetrieveById(objEmployeeCategory);
              txtEmpCategoryName.Text = EmpCategoryDt.Rows[0]["EmpCategoryName"].ToString();
          }
  
          private void LoadDataIntoListBox()
          {
              EmployeeCategory objEmployeeCategory = new EmployeeCategory();
              EmployeeCategoryBLL empCategoryBLL = new EmployeeCategoryBLL();
              DataSet EmployeeCategoryDs = new DataSet();
              EmployeeCategoryDs = empCategoryBLL.RetreiveFromTable();
              DataTable EmployeeCategoryDt = EmployeeCategoryDs.Tables[0];
              DataRow tempRow = null;
              foreach (DataRow tempRow_Variable in EmployeeCategoryDt.Rows)
              {
                  tempRow = tempRow_Variable;
                  string rowText = tempRow["EmpCategoryName"] + "(" + tempRow["ID"] + ")";
                  string rowValue = tempRow["ID"].ToString();
                  listEmployeeCategory.Items.Add(new ListItem(rowText, rowValue));
              }
          }
  
          protected void btnSave_Click(object sender, EventArgs e)
          {
              EmployeeCategory objEmployeeCategory = new EmployeeCategory();
              EmployeeCategoryBLL empCategoryBLL = new EmployeeCategoryBLL();
              objEmployeeCategory.EmpCategoryName = txtEmpCategoryName.Text;
              bool isSave = empCategoryBLL.SaveToTable(objEmployeeCategory);
  
              if (isSave)
              {
                  int id = empCategoryBLL.ReturnLastInsertedId();
                  lblMessage.Visible = true;
                  lblMessage.Style.Add("Color", "Green");
                  lblMessage.Text = "Data saved successfully!";
                  string rowText = txtEmpCategoryName.Text + "(" +id.ToString()+ ")" ;
                  string rowValue = id.ToString();
                  listEmployeeCategory.Items.Add(new ListItem(rowText, rowValue));
                  txtEmpCategoryName.Text = "";
              }
  
              else 
              {
                  lblMessage.Visible = true;
                  lblMessage.Style.Add("Color", "Red");
                  lblMessage.Text = "Data saving failed!";
              }
          }
  
          protected void btnEdit_Click(object sender, EventArgs e)
          {
              if (IsPostBack)
              {
                  EmployeeCategory objEmployeeCategory = new EmployeeCategory();
                  EmployeeCategoryBLL empCategoryBLL = new EmployeeCategoryBLL();
                  objEmployeeCategory.EmpCategoryName = txtEmpCategoryName.Text;
                  objEmployeeCategory.ID = Convert.ToInt32(listEmployeeCategory.SelectedValue);
  
                  bool isEdit = empCategoryBLL.EditInTable(objEmployeeCategory);
                  if (isEdit)
                  {
                      int id = objEmployeeCategory.ID;
                      lblMessage.Visible = true;
                      lblMessage.Style.Add("Color", "Green");
                      lblMessage.Text = "Data edited successfully!";
                  }
                  else
                  {
                      lblMessage.Visible = true;
                      lblMessage.Style.Add("Color", "Red");
                      lblMessage.Text = "Data editing failed!";
                  }
              }
          }
  
          protected void btnDelete_Click(object sender, EventArgs e)
          {
              if (IsPostBack)
              {
                  EmployeeCategory objEmployeeCategory = new EmployeeCategory();
                  EmployeeCategoryBLL empCategoryBLL = new EmployeeCategoryBLL();
                  objEmployeeCategory.ID = Convert.ToInt32(listEmployeeCategory.SelectedValue);
                  bool isDelete = empCategoryBLL.DeleteFromTable(objEmployeeCategory);
  
                  if (isDelete)
                  {
                      lblMessage.Visible = true;
                      lblMessage.Style.Add("Color", "Green");
                      lblMessage.Text = "Data deleted successfully!";
                      listEmployeeCategory.Items.Remove(new ListItem(listEmployeeCategory.SelectedItem.Text, listEmployeeCategory.SelectedValue));
                      txtEmpCategoryName.Text = "";
                  }
  
                  else
                  {
                      lblMessage.Visible = true;
                      lblMessage.Style.Add("Color", "Red");
                      lblMessage.Text = "Data deleting failed!";
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>The <code>txtEmpCategoryName.Text</code> is still containing the old value, so whenever I try to update, it only takes the old value and not the modified value in the textbox. It seems that the TextChanged event is not working. What is the fix to this problem?</p>