TextBox value is not updating
I'm using following DropDownList event to select an employee from MS SQL Server 2005 and showing the employee's information on TextBox.
protected void employeeDropDownList_SelectedIndexChanged(object sender,
EventArgs e)
{
EmployeeDAL employeeDAL = new EmployeeDAL();
DataTable dataTable = employeeDAL.GetEmployeeData();
for (int i = 1; i <= dataTable.Rows.Count; i++)
{
if (Convert.ToInt开发者_Go百科32(employeeDropDownList.SelectedValue) == i)
{
nameTextBox.Text = dataTable.Rows[i-1]["employee_Name"].ToString();
useNameTextBox.Text=dataTable.Rows[i-1]["employee_UserName"].ToString();
addressTextBox.Text=dataTable.Rows[i-1]["employee_Address"].ToString();
break;
}
}
}
Then I'm using the following Button event to update Employee information.
protected void employeeUpdateButton_Click(object sender, EventArgs e)
{
EmployeeDAL employeeDAL = new EmployeeDAL();
EmployeeDAO employeeDAO = new EmployeeDAO
{
EmployeeID = Convert
.ToInt32(employeeDropDownList.SelectedValue),
Name = nameTextBox.Text,
Username = useNameTextBox.Text,
Address = addressTextBox.Text,
};
employeeDAL.UpdateEmployee(employeeDAO);
}
But the problem is... values of the TextBoxes are not changing. I mean TextBoxes are keeping previous values those were assigned in employeeDropDownList_SelectedIndexChanged event. But why? What should I do now?
The button click is being executed before the text fields have been updated, put a break point in both eventhandlers. Put a watch on the textbox and on you dao object and watch how they are updated when yhou post back. I really don't think it is a data issue but do not totally discount it, just seems, to me at least, an asp.net issue with caching the textbox values between postbacks.
Ok finally I got the answer but can't explain it. For the DropdownList I used the following code:
and the main culprit was this OnLoad event. When I'm running the application without this OnLoad event then my problem is solved. Could anyone explain what was wrong with this OnLoad event?
Well, since we don't have the implementation of your EmployeeDAL class, it's a little hard to know exactly what the problem is. Here are a few things I would check:
- Make sure the GetEmployeeData function doesn't implement some caching internally.
- Check the UpdateEmployee function to verify that it does actually commit to the database. Sometimes update functions only change things in memory and there is a different function to actually commit those changes to the store.
- In your employeeUpdateButton_Click function, double check the value you are setting to the EmployeeID property.
- Run SQL Profiler to see exactly what T-SQL is hitting the database.
精彩评论