p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET 4 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=560)
-   -   Page_Prerender (http://p2p.wrox.com/showthread.php?t=83683)

markhh May 17th, 2011 04:07 AM

Page_Prerender
 
Hi all, can anyone tell me the exact sequence the below code runs in. The line
this.PreRender += newEventHandler(Page_Prerender); in particular, what is it, what does it do and at what stage does it run

Correct me if i am wrong but in the BasePage class the Page_Prerender method is called first through the page life cycle... what goes on next in the below code. I am ok with Comparing the contents of the Title bit.

Many thanks
Mark




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
///<summary>
///
Summary description for BasePage
///</summary>
publicclassBasePage : System.Web.UI.Page
{
//no instance variables
public BasePage()
{
this.PreRender += newEventHandler(Page_Prerender);
// TODO: Add constructor logic here
}
//methods
privatevoid Page_Prerender (Object sender, EventArgs e)
{
if (this.Title == "Untitled page" || String.IsNullOrEmpty (this.Title))
{
thrownewException ("Page does not have a title....");
}
}
}

disel2010 May 17th, 2011 06:21 AM

Hi,

the PreRender event triggers right after the init and load events are completed and before the render and unload event.

Which means that this event triggers after your page (and child controls) have initialized, viewstate has been reconstructed and postback events have been processed. Looks like a logic place to put a check on empty page title.

Hope this helps..

markhh May 17th, 2011 06:53 AM

Hi Disel2010, your reply is much appreciated but my original questions stand. Does the Page_Prerender method get called automatically by the page life cycle or does the this.PreRender += newEventHandler(Page_Prerender); in the constructor call the method. I seem to be having a brain freeze with this one.

Thanks again
Mark



Imar May 17th, 2011 06:56 AM

Hi Mark,

The event is raised by the Page class automatically. The code in the constructor then assigns a method that will be called to handle the event.

Hopenthis helps,

Imar

markhh May 17th, 2011 10:00 AM

Hi Imar and many thanks for your reply. "The event is raised by the Page class automatically.!" the event being the Page_Prerender. That's good, i am getting there.

"The code in the constructor then assigns a method that will be called to handle the event." As below
public BasePage()
{
this.PreRender += newEventHandler(Page_Prerender);
}


How does the page class know to run the BasePage() constructor?. I guess this is where my problem is. I know this is wrong but the way I am looking at it the code should be something like, the Page_Prerender method called from within the Page_Prerender class.

Public Page_Prerender()
{
// Page_Prerender code
}


Many thanks
Mark

Imar May 17th, 2011 10:09 AM

The Page class *is* the BaseClass. When you create a page, you let it inherit BasePage. So, the request for a page is handled by your BasePage. Its base class (eg. System.Web.UI.Page) then raises the event which is handled by the method you assign in the constructor of the BasePage class.

So, actually, Page and BasePage are more or less merged into a single class. The BasePage can do whatever Page can (but not vise versa) including handling the PreRender event.

Does this clarify things?

Imar

markhh May 20th, 2011 06:15 AM

Yes, this makes it clear.

Many thanks
Mark


All times are GMT -4. The time now is 10:56 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.