why controls keep their state even on a different page?

B

Bisser Milanov

I have
1. A main form with 2 buttons (btn1 and btn2) and a place holder (ph)
2. A Web User Control (ctl1) containing a textbox (txt)
3. A Web User Control (ctl2) containing a textbox (txt)

When I press btn1 I have the following code:
ph.Controls.Add(LoadControl("ctl1.ascx"))

When I press btn2 I have the following code:
ph.Controls.Add(LoadControl("ctl2.ascx"))

In ctl1 I have the following line on form load:
txt.Enabled = False


When I press btn1 ctl1 is loaded and the txt is disabled. When I press btn2
ctl2 is loaded and the txt is disabled. Why the txt on ctl2 is disabled? and
how can I avoid it? Somehow it remembered the state of the textbox from the
previous loaded page. Why?
 
P

Paul Wilson [MVP]

Viewstate is based on a control's position in the hierarchical control tree
and you are loading different controls into the same position each time.
You either need to setup different placeholders so that each set of controls
is loaded into a different placeholder, or manually deal with consequences.

Thanks, Paul Wilson (MVP)
 
B

Bisser Milanov

Hm, what is the purpose of a Place Holder then? If I will load always the
same control in this placeholder I will simply put the control itself on the
page. Why would I put it into a placeholder?
I actually tried to clear the viewstate when I load different control but it
didn't work. I tried to clear the view state of the control of the main form
of the text box itself but nothing worked. Any ideas how to clear the view
state?
 
G

gerry

forget the 'position' stuff give the conflicting controls different names
that has solved this problem for us.
 
P

Paul Wilson

If you have a different placeholder for each possible user control you
may load then the relative positions will be unique. By the way, unique
control ways does not work with viewstate, although it will work if your
problem is just the posted values.

Thanks, Paul Wilson (MVP)
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top