TextBox.Text Prints Outdated Text

W

william.oram

Suppose I have a button that, when pressed, takes the current value of
TextBox.Text pulled from SQL and sends it to Label1.Text. This code
should be:

private void update_Click(object sender, System.EventArgs e)
{
Label1.Text = TextBox1.Text;
}

As the program currently runs, clicking the button sends TextBox's
*first* value to Label1, not what was typed over it. That is, if
TextBox initially reads 'porkchop', change it to 'sandwiches' and
click the button, the label will read 'porkchop.'

My only idea of a solution is toggling AutoPostBack, but that didn't
help any. What else can I try?

Thanks.
 
A

Alberto Poblacion

private void update_Click(object sender, System.EventArgs e)
{
Label1.Text = TextBox1.Text;
}

As the program currently runs, clicking the button sends TextBox's
*first* value to Label1, not what was typed over it. That is, if
TextBox initially reads 'porkchop', change it to 'sandwiches' and
click the button, the label will read 'porkchop.'

My only idea of a solution is toggling AutoPostBack, but that didn't
help any. What else can I try?

Take a look at your Page_Load event. This always fires *before* the
Click event. If you are reloading the textbox from the database inside
Page_Load, it will overwrite whatever the user typed before you can pick it
up in the update_Click event. If this is what is happening, you can use an
"if (!Page.IsPostBack)" inside Page_Load to avoid executing that part of the
code during the postback.
 
W

william.oram

Take a look at your Page_Load event. This always fires *before* the
Click event. If you are reloading the textbox from the database inside
Page_Load, it will overwrite whatever the user typed before you can pick it
up in the update_Click event. If this is what is happening, you can use an
"if (!Page.IsPostBack)" inside Page_Load to avoid executing that part of the
code during the postback.

That did it. Thanks so much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top