Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Determining 'first load' state of a dynamically inserted control

Reply
Thread Tools

Determining 'first load' state of a dynamically inserted control

 
 
John Burke
Guest
Posts: n/a
 
      05-05-2004
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


 
Reply With Quote
 
 
 
 
John Burke
Guest
Posts: n/a
 
      05-05-2004

"Scott Mitchell [MVP]" <(E-Mail Removed)> wrote in message
news:aJZlc.45146$(E-Mail Removed) m...
> 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.velocityreviews.com/forums/(E-Mail 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


 
Reply With Quote
 
 
 
 
Victor Garcia Aprea [MVP]
Guest
Posts: n/a
 
      05-05-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView Dynamically Inserted Template with RadioButton gauty ASP .Net Web Controls 1 04-29-2010 10:27 AM
How can I access the dynamically inserted applet from another applet? huyi0912@hotmail.com Java 1 11-19-2007 03:38 AM
IE: apply stylesheet in dynamically inserted nodes Alvaro G Vicario Javascript 2 08-31-2005 06:30 AM
Adding Rows Dynamically into the Table Web Control but only one row is getting inserted lucky ASP .Net 0 01-12-2005 10:45 AM
ID of dynamically inserted user control Bijoy Naick ASP .Net 1 11-17-2004 08:49 PM



Advertisments