Using IsPostBack in Page_Load of dynamically added control

Discussion in 'ASP .Net' started by DC, Nov 30, 2007.

  1. DC

    DC Guest

    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
    DC, Nov 30, 2007
    #1
    1. Advertising

  2. 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").


    "DC" <> wrote in message
    news:...
    > 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
    Scott Roberts, Nov 30, 2007
    #2
    1. Advertising

  3. DC

    DC Guest

    On 30 Nov., 16:23, "Scott Roberts" <-webworks-
    software.com> wrote:
    > 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").
    >
    > "DC" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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- 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
    DC, Dec 3, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bennett Haselton
    Replies:
    1
    Views:
    504
    John M Deal
    Nov 8, 2004
  2. =?Utf-8?B?U2FtdWVs?=
    Replies:
    7
    Views:
    1,028
    Scott Allen
    Aug 10, 2005
  3. Joey
    Replies:
    2
    Views:
    3,914
  4. Replies:
    3
    Views:
    5,360
  5. Joshua Gunder via .NET 247

    Listbox in Custom Control doesn't keep listitems added on Page_Load of container page

    Joshua Gunder via .NET 247, May 13, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    204
    Teemu Keiski
    May 13, 2004
Loading...

Share This Page