Using IsPostBack in Page_Load of dynamically added control

D

DC

Hi,

I am dynamically adding a usercontrol that uses "this.IsPostBack" in
Page_Load to decide whether it must populate some of it's inner
controls or not. Since I am adding the usercontrol to the page at
runtime (on response to a click event), IsPostBack is always false and
the inner controls never get populated.

I changed "IsPostBack" to "IsInitial" now, using this property:

public bool IsInitial
{
get
{
if (ViewState["IsInitial"] == null)
{
ViewState["IsInitial"] = false;
return true;
}
return false;
}
}

I don't think this is the way to do it. If the sourcecode of the
control is not available, I cannot add the usercontrol dynamically
without running into this problem. Is there a way to explicitly set
"IsPostBack" of the control to "true" from outside somehow? I thought
using "if (!IsPostBack)" to init controls in Page_Load is recommended.

Kind regards
DC
 
S

Scott Roberts

I believe that dynamically added controls must be created in Page_Init in
order to have their properties set automatically from the viewstate. I'd
image the IsPostBack property works the same way.

One option is to add the user control at design time then simply show/hide
it at runtime. Another option is to always add the user control in Page_Init
then remove it (in, say, PreRender) if it is not ultimately needed. Finally,
you could keep your "IsInitial" viewstate property but check it in Page_Init
and create the user control if it is true (and maybe rename it to
"LoadUserControl").
 
D

DC

I believe that dynamically added controls must be created in Page_Init in
order to have their properties set automatically from the viewstate. I'd
image the IsPostBack property works the same way.

One option is to add the user control at design time then simply show/hide
it at runtime. Another option is to always add the user control in Page_Init
then remove it (in, say, PreRender) if it is not ultimately needed. Finally,
you could keep your "IsInitial" viewstate property but check it in Page_Init
and create the user control if it is true (and maybe rename it to
"LoadUserControl").




I am dynamically adding a usercontrol that uses "this.IsPostBack" in
Page_Load to decide whether it must populate some of it's inner
controls or not. Since I am adding the usercontrol to the page at
runtime (on response to a click event), IsPostBack is always false and
the inner controls never get populated.
I changed "IsPostBack" to "IsInitial" now, using this property:
public bool IsInitial
{
get
{
if (ViewState["IsInitial"] == null)
{
ViewState["IsInitial"] = false;
return true;
}
return false;
}
}
I don't think this is the way to do it. If the sourcecode of the
control is not available, I cannot add the usercontrol dynamically
without running into this problem. Is there a way to explicitly set
"IsPostBack" of the control to "true" from outside somehow? I thought
using "if (!IsPostBack)" to init controls in Page_Load is recommended.
Kind regards
DC- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Thank you, Scott!

I tried loading the control in OnInit but the only difference I got is
that I cannot do the loading depending on some other controls that
depend from viewstate (since they will not have their viewstate
processed yet). The dynamically loaded control does still always have
"IsPostBack == true".

I am quiet convinced that the design of my application is flawed. What
I want to design is a MultiView with rather large fat controls in
every view. I wanted to avoid that all the applications in all the
views execute Page_Load on all PostBacks - although they are actually
hidden.

So I thought it was smart to dynamically load the control only when
the actual View of the control was the active view. Of course, the
control that I would then dynamically load into the View must have
"IsPostBack == false" on the initial load on "IsPostBack == true" on
subsequent loads of the same View.

So what I want is like a page in a page, kind of like I was loading
the control in the view into an Iframe and have it live its private
life. Once another View is activated, the dynamically loaded control
is allowed to loose its state.

But I think that's not a good design in ASP.Net since the architecture
relies on one state for the whole page (it is a similar problem like
the "one serverside form per page only" limitation). So I am better of
to plant all the controls into their views at designtime and get the
advantage of retaining their state (if that is desired). I will do
that now and measure the actual performance hit.

Regards
DC
 

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,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top