Determining 'first load' state of a dynamically inserted control

J

John Burke

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
 
J

John Burke

Scott Mitchell said:
John said:
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
(e-mail address removed)
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
 
V

Victor Garcia Aprea [MVP]

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
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top