PlaceHolder ViewState

P

paps

I need to programmatically change an ASCX control into a PlaceHolder during
PostBack.
When i change the PH control during PageLoad like :
MyCtrl = LoadControl(path);
PlaceHolder.Controls.Add(ctrl);

the PlaceHolder load the old ViewState information and the loaded control
isn't correct.

How can i solve this porblem ?

I've also tryied to clear view state of the PH but nothing happen


thanks
Paps
 
B

Brock Allen

Try rebuilding the control in Page_Init not Page_Load. Page_Load is called
after ASP.NET reloads your controls from the postdata and viewstate, so Page_Load
is typically too late.
 
P

paps

I've tried but it doesn't work...

Brock Allen said:
Try rebuilding the control in Page_Init not Page_Load. Page_Load is called
after ASP.NET reloads your controls from the postdata and viewstate, so Page_Load
is typically too late.
 
P

paps

here an ex:
private void Page_Load(object sender, System.EventArgs e)
{
Control ctrl;
if (!IsPostBack)
ctrl = (Control)Page.LoadControl("WebUserControl1.ascx");
else
ctrl = (Control)Page.LoadControl("WebUserControl2.ascx");

PlaceHolder1.Controls.Add(ctrl);
}

if both UserControl have (for example) a TextBox1 control and i set a value
into the first one after postback I found the same value of
(WebUserControl1.ascx) TextBox1 into the (WebUserControl2.ascx) TextBox2
instead an empty value

hope it is more clear.

thanks
Paps
 
B

Brock Allen

Ok, I see. I imagine what's happening is that ASP.NET is trying to assign
in all of the posted data into your USerControl2 when in fact you don't want
that. So, one suggestion is to assign the UserControls different IDs:

private void Page_Load(object sender, System.EventArgs e)
{
Control ctrl;
if (!IsPostBack)
{
ctrl = (Control)Page.LoadControl("WebUserControl1.ascx");
ctrl.ID = "UC1";
}
else
{
ctrl = (Control)Page.LoadControl("WebUserControl2.ascx");
ctrl.ID = "UC2";
}

PlaceHolder1.Controls.Add(ctrl);
}

This should make ASP.NET able to distinguish between them when trying to
reload viewstate. HTH
 
T

Teemu Keiski

Hi,

Just to add that postback data loading happens also after Page_Load, for
those controls added dynamically in Page_Load. Postback data loading is
tried in two phases once before Page_Load and second time after (for that
postback data which is leftover from the first try)

And ViewState loading happens also when control is added to the control
collection (if control is added after LoadViewState phase of the Page),
means that dynamically adding controls in Page_Load is still fine what comes
to loading viewstate and postback data. E.g ViewState loading after Page has
passed LoadViewState happens by the control collection, so it is not fixed
to Page's life cycle phase (but postback data loading is, it happens fixed
two times based on Page's phase)

I've discussed this on ASP.NET Forums, if someone is interested:
http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=151590
 
T

Teemu Keiski

Basically loading ViewState happens just based on current index in control
(and state) tree. First control's viewstate data is tried to load to the
second one because they are on the same index. But with postback data
loading it is because they have same dynamically generated ID which comes
also based on control's index in control collection (postback data is loaded
based on UniqueID by the Page). So let's not mix viewstate and postback data
here, they are different things.

As is about loading postback data (TextBox keeping its Text value), Brock's
suggestion about the different ID should work just fine.
 

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,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top