My button_click function doesn't get current value

C

Cindy Lee

I have an image button that I do an button_click with

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
stringt value1=TextBox1.Text;

I get the value here ok, but only if I don't set it in the pageload part.
TextBox7.Text="a"; // if this is in the pageload, I will always get "a"

I want to be able to change the value on the page, and have it show up in
ImageButton1_Click. Anyone know why this is happening.
 
S

Scott Roberts

By the time the Page_Load event occurs, TextBox7 has already been loaded
with the value entered on the page. So when you set TextBox7.Text = "a" you
are overwriting the value entered by the user.

The general practice for initializing control values in Page_Load is as
follows:

if (!IsPostBack)
{
TextBox7.Text = "a";
}

That way the control value is only initialized on the initial page load, but
not on any postbacks.

Scott
 
C

Cindy Lee

Ahh, thanks. cleared things up.

Scott Roberts said:
By the time the Page_Load event occurs, TextBox7 has already been loaded
with the value entered on the page. So when you set TextBox7.Text = "a" you
are overwriting the value entered by the user.

The general practice for initializing control values in Page_Load is as
follows:

if (!IsPostBack)
{
TextBox7.Text = "a";
}

That way the control value is only initialized on the initial page load, but
not on any postbacks.

Scott
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top