Wrox Programmer Forums
|
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
 
Old May 17th, 2011, 04:07 AM
Authorized User
 
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
Default 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....");
}
}
}
 
Old May 17th, 2011, 06:21 AM
Authorized User
 
Join Date: Jan 2011
Posts: 86
Thanks: 1
Thanked 12 Times in 12 Posts
Default

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

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


 
Old May 17th, 2011, 06:56 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old May 17th, 2011, 10:00 AM
Authorized User
 
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
Default

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
 
Old May 17th, 2011, 10:09 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
markhh (May 20th, 2011)
 
Old May 20th, 2011, 06:15 AM
Authorized User
 
Join Date: Apr 2005
Posts: 71
Thanks: 23
Thanked 0 Times in 0 Posts
Default

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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.