Controls reset after postback, why?

G

Guest

I have few buttons on my ASP.NET page which are supposed to hide and show
pannels. On the initial load everything is properly populated but then when I
press one of the buttons, which are supposed to hide one and show another
pannel, the state of a button goes from disabled (on the initial load) to
enabled (wrong). Same thing with one of the labels which is set to a
particular value first time around but after postback goes back to an empty
string.

Button state, label value and additional texbox values are set during
initial load. Now, all the text boxes hold their values between postbacks
but not the button and the label. What could be wrong?

I have some textboxes populated during initial page load event
 
G

Guest

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button2.Enabled = false;
Panel1.Visible = false;
}

}
protected void Button1_Click(object sender, EventArgs e)
{
Button2.Enabled = true;
Panel2.Visible = true;
Panel1.Visible = false;
}

This is what's going on when I run this code;
1. Page loads w/ controls as expected.
2. When I hit Button1:
- pannels are swaped and Button2 is now Enabled (should be disabled)
3. I hit Button2 (which was Enabled above and doesn't even have an event
handler implemented) and now both Pannels show up.
WTF .. beats me. Any help would be appreciated.

PS. I don't know if this matters but this page inherits from Master page
(global to the whole application).
 
J

Jimi200478

okay....i see what your problem is. your code should be like this:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button2.Enabled = false;
Panel1.Visible = false;
}

}
protected void Button1_Click(object sender, EventArgs e)
{
//i removed the "Button2.Enabled = true" since you dont want it
to be enabled.
Panel2.Visible = true;
//you can remove this line. viewstate will remember the state
of panel1
Panel1.Visible = false;
}

your other problem with both panels showing up is because even though
you have not assigned an event to button2, it is a button, and by its
nature it will perform a postback. since your page_load only performs
that logic if the page has not been posted back, when you click button2
(even without an event), that code in page_load will not fire because
the page has been posted back.

i dont know if that is easy to understand....ill be here if you need
more clarification.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top