Reputation: 2140
I fill my texboxes with:
foreach (User u in userInfo)
{
txtNickname.Text = u.Nickname;
txtFirstName.Text = u.FirstName;
txtLastName.Text = u.LastName;
txtEmail.Text = u.Email;
}
My textboxes are now filled with the data from the database.
For example I fill new values in all of the texboxes and I click on the button, than happens this:
if (txtNickname.Text != String.Empty && txtFirstName.Text != String.Empty && txtLastName.Text != String.Empty && txtEmail.Text != String.Empty)
{
//TODO
}
But when I debug: the values of the textboxes are the old values (the values from the foreach-loop), and not the new values which I fill in the textboxes.
Why is this happening? I'm loading some data from the database in the textboxes, after that I'm changing the values of the textboxes myself, and when I debug the textbox values are still the database-values (see foreach-loop).
Upvotes: 1
Views: 193
Reputation: 216293
If the first loop is executed in the Page_Load event then you should be sure that you don't execute again when the page is posted back as result of clicking on the button.
More info in Page.IsPostBack on MSDN
private void Page_Load()
{
if (!IsPostBack)
{
// This code should be executed only when the page is being
// rendered for the first time not when is responding to a postback
// raised by the <runat="server"> controls
UserInfoCollection userInfo = GetUserInfoCollection();
foreach (User u in userInfo)
{
txtNickname.Text = u.Nickname;
txtFirstName.Text = u.FirstName;
txtLastName.Text = u.LastName;
txtEmail.Text = u.Email;
}
}
}
Upvotes: 3