Understanding Page Inheritence in ASP.Net 2.0

1 view
Skip to first unread message

BabuLives

unread,
Oct 15, 2005, 3:27:55 AM10/15/05
to .NetIndia
hai groups..

see this page...

http://west-wind.com/weblog/posts/3016.aspx


its really very gud...

Regards,
Satheesh

Shivprasad Koirala

unread,
Oct 15, 2005, 6:21:59 AM10/15/05
to .NetIndia
Good one Babu , Explain page inheritance in depth

-------
Regards ,
C#, VB.NET , SQL SERVER , UML , DESIGN Patterns Interview question book
http://www.geocities.com/dotnetinterviews/
My Interview Blog
http://spaces.msn.com/members/dotnetinterviews/

BabuLives

unread,
Oct 17, 2005, 3:54:52 AM10/17/05
to .NetIndia
Like we can create a base class that inherits Page Class and we can add
some common fuctionalities to this class and we can make this class to
be inherited by our Webform class..ie..

public class WebForm1 : MyBaseClass
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

...
}


where MyBaseClass is,

public class MyBaseClass : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
// ... add custom logic here ...

// Be sure to call the base class's OnLoad method!
base.OnLoad(e);
}
}

Why We need a Base Class???

A base class is advantageous if there is some common functionality
needed to be required on every ASP.NET page in your Web application.
That is, if you find yourself repeatedly copying and pasting the same
snippet of code into most, if not all, ASP.NET pages in your
application, you should seriously consider using a base class. With a
base class you can define this common behavior in one spot. The ASP.NET
page's that inherit from this base class, then, will automatically
perform the needed functionality. If there needs to be a change to this
common logic, you'll only need to change it in one place - the base
class - as opposed to having to change the code in all the ASP.NET
pages.


What will happen in the Server?

Normally the page parser first parses the aspx page ie html& script
page and create a class out of it and makes this class to inherit the
code behind class (aspx.cs) and in turn inherits our base class which
inherits the Page Class.

Like..


Class htmlClass:WebForm1.aspx.cs (out of the html and scripts)
{

}


Regards,
Satheesh

Reply all
Reply to author
Forward
0 new messages