New LoadFinished event for Page

Note: this entry has moved.

Lots of people starting to learn ASP.NET tend to heavily depend on the Load event. They find quite naturally to write most core logic into Load until they realize their code just doesn’t work as expected and start coding all kind of hacks.

 

A common example of this mistake is to write code in Load that depends on values that will be updated only after postback data has been processed; but as Load fires before postback data (2nd try) and RaiseChangedEvents and RaisePostBackEvent are processed such code won’t work properly -- and that is when people start writing hacks like peeking at the Forms collection to get the updated value; the WebForm model is not followed and bad things happen when you do that J

 

If you still doesn’t get the scenario I’m talking about take a look at this post.

 

The solution to this? Move your code to PreRender, sure; but this doesn’t sound very intuitive to a beginner.

 

I believe that adding a brand new event to Page, something like LoadFinished that fires right after 2nd try postback data and changed & postback events are processed and right before PreRender would be a more natural choice where to put such code.

 

You could then market this new event as the one that fires when the page has completely finished its loading and the place where any code can safely depend on all postback data to be updated.

 

 

Published Sunday, May 25, 2003 3:11 AM by vga

Comments

# re: New LoadFinished event for Page

Saturday, May 24, 2003 11:54 PM by Frans Bouma
shouldn't code relying on a postback (f.e. a button is clicked) be placed in a routine that is fired from the eventhandler of that button f.e.?

# re: New LoadFinished event for Page

Sunday, May 25, 2003 5:47 AM by Paschal
Frans yes but the Page load event is still executed whatever the Event you link to the button.
I use intensively now Prerender event, but the event cycle can be quite complex when you have all your controls with their own events.

# re: New LoadFinished event for Page

Sunday, May 25, 2003 10:48 AM by vga
Hi Frans,

Not always. The code that goes into an eventhandler is just for handling that specific event and won't run on every postback (ie. if the button wasn't clicked). So you still need a place where to put "general" code that must always run and depend on the updated values (ie. PreRender).

# re: New LoadFinished event for Page

Monday, May 26, 2003 8:24 AM by Nikhil Kothari
Yep, we've added an event for this purpose - LoadComplete.

We've also added various other events in the page lifecycle that should be useful to page and control developers.

# re: New LoadFinished event for Page

Thursday, May 29, 2003 12:58 AM by venu
this is testing

# re: New events! [To Nikhil]

Saturday, May 31, 2003 1:14 PM by Gauthier
FROM Nikhil Kothari:
>Yep, we've added an event for this purpose -
>LoadComplete.
>
>We've also added various other events in the
>page lifecycle that should be useful to page
>and control developers.

I've a some Event schemes for different control type that I've developped myself, for exemple I've a Skinned usercontrol that does that:
CreateChildControls():
-LoadSkin();
-InitializeSkin();
-RegisterEvents();
-Controls.Add(this.skin);

another one is a simple Databound scheme:
OnDataBinding(EventArgs e)
-EnsureEditorMode();
-EnsureRights();
-EnsureChildControls();
-if(!Page.IsPostBack)
- BindItemToControl();
-else
- BindControlToItem();

antoher one for the databoundtemplatedlist controls...

Does some of the new events will help to maintain a simple event scheme?

I find that some event schemes I use are very usefull when implementing multiple similar controls from an abstract class, and since I'm interested in such schemes I would like to know a bit more on your comment.

Thanks!

# re: New LoadFinished event for Page

Sunday, June 20, 2004 4:24 PM by Andreas Rudischhauser
Move To PreRender..... :-S

Isn't it better to check in PreRender if the Property changed and then call EnsureChildControls or soemthing?

# re: New LoadFinished event for Page

Tuesday, September 12, 2006 10:45 PM by Sabrina

As an ASP.NET 1.1 solution, what I do is, in the Base Page that other aspx pages derive from, I override the RaisePostBackEvent() method and raise a custom event. This way, even UserControls on the Page can wire handlers to the event to respond at the appropriate time. Your thoughts?

protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)

{

  base.RaisePostBackEvent (sourceControl, eventArgument);

  this.OnPostBackEventsComplete(new EventArgs());

}

# re: New LoadFinished event for Page

Saturday, August 04, 2007 4:57 PM by diller

nice  site...  look on my site

http://www.desklamp.fora.pl - desk lamp

http://www.lampshades.fora.pl - lamp shades

# re: New LoadFinished event for Page

Tuesday, December 02, 2008 11:57 AM by Asina

<a href= bestpre.com ></a>

# re: New LoadFinished event for Page

Friday, December 05, 2008 7:00 PM by Semil

<a href= spiritez.com ></a>

# re: New LoadFinished event for Page

Friday, December 26, 2008 5:59 AM by jyner_gz

<a href= membres.lycos.fr/maffals >genetic disorters</a>

# re: New LoadFinished event for Page

Sunday, January 25, 2009 12:55 PM by Serega4356

������� ����� ������ � ������� ������� � ���������. ���� ��������� �������� <A href="gorasofta.spb.ru/"target=_blank>

<FONT size=1>���� �����</FONT></A>

# re: New LoadFinished event for Page

Saturday, February 28, 2009 11:00 PM by jyner_wv

<a href= http://adultchatsfinder.com >find partner</a>

# re: New LoadFinished event for Page

Thursday, April 09, 2009 12:13 PM by nick_ricchi

www.message_chicobo.com

# re: New LoadFinished event for Page

Wednesday, April 22, 2009 6:21 PM by Mery-hi

<a href= <div align=center><h3>Error. Page cannot be displayed. Please contact service provider for more details.</h3></div> ></a>

# re: New LoadFinished event for Page

Wednesday, April 29, 2009 6:53 PM by Mery-cn

<a href= <div align=center><h3>Error. Page cannot be displayed. Please contact service provider for more details.</h3></div> ></a>

Leave a Comment

(required) 
(required) 
(optional)
(required)