 |
BOOK: Beginning ASP.NET 4 : in C# and VB
 | This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

May 17th, 2011, 04:07 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
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....");
}
}
}
|
|

May 17th, 2011, 06:21 AM
|
|
Authorized User
|
|
Join Date: Jan 2011
Posts: 86
Thanks: 1
Thanked 12 Times in 12 Posts
|
|
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..
|
|

May 17th, 2011, 06:53 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 17th, 2011, 06:56 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

May 17th, 2011, 10:00 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 17th, 2011, 10:09 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

May 20th, 2011, 06:15 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
|
|
Yes, this makes it clear.
Many thanks
Mark
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Issue with Page_PreRender |
webdeveloper |
C# |
0 |
October 17th, 2007 12:37 PM |
|
 |
|