Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Determining 'first load' state of a dynamically inserted control

8 views
Skip to first unread message

John Burke

unread,
May 4, 2004, 9:00:14 PM5/4/04
to
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


Scott Mitchell [MVP]

unread,
May 5, 2004, 12:04:22 AM5/5/04
to

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
mitc...@4guysfromrolla.com
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!

John Burke

unread,
May 5, 2004, 3:02:30 AM5/5/04
to

"Scott Mitchell [MVP]" <mitc...@4guysfromrolla.com> wrote in message
news:aJZlc.45146$KV2...@newssvr29.news.prodigy.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


Victor Garcia Aprea [MVP]

unread,
May 5, 2004, 12:23:09 PM5/5/04
to
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" <n...@offence.net> wrote in message
news:ObyqLvjM...@tk2msftngp13.phx.gbl...

0 new messages