Determining 'first load' state of a dynamically inserted control

Discussion in 'ASP .Net Building Controls' started by John Burke, May 5, 2004.

  1. John Burke

    John Burke Guest

    Hi Group,

    Is their an existing convenient method for determining if a usercontrol has
    been loaded for the first time? The usercontrol itself may be initially
    loaded on subsequent postbacks, not the initial page access.

    I am assuming I cannot use IsPostBack, as that appears to refer only to the
    page rather than the usercontrol. In other words, controls contained within
    the dynamically loaded usercontrol cannot rely on IsPostBack to determine
    that this is the first time they have been loaded. I have not experimented
    with this yet, so it would be nice if I was completely and utterly wrong in
    this assumption.

    Thanks.
    JB
     
    John Burke, May 5, 2004
    #1
    1. Advertising

  2. John Burke

    John Burke Guest

    "Scott Mitchell [MVP]" <> wrote in message
    news:aJZlc.45146$...
    > John Burke wrote:
    > > Is their an existing convenient method for determining if a usercontrol

    has
    > > been loaded for the first time? The usercontrol itself may be initially
    > > loaded on subsequent postbacks, not the initial page access.
    > >
    > > I am assuming I cannot use IsPostBack, as that appears to refer only to

    the
    > > page rather than the usercontrol. In other words, controls contained

    within
    > > the dynamically loaded usercontrol cannot rely on IsPostBack to

    determine
    > > that this is the first time they have been loaded. I have not

    experimented
    > > with this yet, so it would be nice if I was completely and utterly wrong

    in
    > > this assumption.

    >
    > Hi John. Your assumption about IsPostBack is correct. In fact, if you
    > use Reflector to examine the source code of the
    > System.Web.UI.UserControl class, you'll see that the IsPostBack property
    > simply returns the base Page's IsPostBack property value:
    >
    > public bool get_IsPostBack()
    > {
    > return base.Page.IsPostBack;
    > }
    >
    >
    > What you can do is on Page_Load check to see if a variable exists in the
    > ViewState StateBag. If it doesn't, you can assume it's the first load,
    > otherwise it has been posted back. This works assuming that you create
    > this view state variable at the end of the User Control's Page_Load.
    >
    > Something like the following (in the User Control code-behind class):
    >
    > protected void Page_Load(...)
    > {
    > if (IsFirstLoad)
    > // do something
    >
    > IsFirstLoad = true;
    > }
    >
    > private bool IsFirstLoad
    > {
    > get
    > {
    > object o = ViewState["MyUC-FirstLoad"];
    > return o == null;
    > }
    > set
    > {
    > ViewState["MyUC-FirstLoad"] = true;
    > }
    > }
    >
    >
    > That ought to do it! :)
    >
    > Happy Programming!
    >
    > --
    >
    > Scott Mitchell
    >
    > http://www.4GuysFromRolla.com
    > http://www.ASPFAQs.com
    > http://www.ASPMessageboard.com
    >
    > * When you think ASP, think 4GuysFromRolla.com!


    Hi Scott,

    Thanks for your considered reply. I figured it would come to that. I had
    imagined it might be possible to imply first load if the control did not
    have LoadViewState called, but this does look like a reliable method.

    JB
     
    John Burke, May 5, 2004
    #2
    1. Advertising

  3. Hi John,

    You can override LoadViewState and set a flag or something. You can count on
    LoadViewState not being called for the first request.

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    "John Burke" <> wrote in message
    news:...
    > Hi Group,
    >
    > Is their an existing convenient method for determining if a usercontrol

    has
    > been loaded for the first time? The usercontrol itself may be initially
    > loaded on subsequent postbacks, not the initial page access.
    >
    > I am assuming I cannot use IsPostBack, as that appears to refer only to

    the
    > page rather than the usercontrol. In other words, controls contained

    within
    > the dynamically loaded usercontrol cannot rely on IsPostBack to determine
    > that this is the first time they have been loaded. I have not

    experimented
    > with this yet, so it would be nice if I was completely and utterly wrong

    in
    > this assumption.
    >
    > Thanks.
    > JB
    >
    >
     
    Victor Garcia Aprea [MVP], May 5, 2004
    #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. Bijoy Naick

    ID of dynamically inserted user control

    Bijoy Naick, Nov 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    414
    Kikoz
    Nov 17, 2004
  2. lucky
    Replies:
    0
    Views:
    525
    lucky
    Jan 12, 2005
  3. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,828
    Matt Berther
    Mar 5, 2005
  4. =?Utf-8?B?UmV6YSBOYWJp?=

    Load User Control Dynamically, Cast object dynamically

    =?Utf-8?B?UmV6YSBOYWJp?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,386
    Scott Allen
    Mar 4, 2005
  5. Replies:
    1
    Views:
    565
    Andrew Thompson
    Nov 19, 2007
Loading...

Share This Page