Inherit class from page, usercontrol, and masterpage - asp.net

I have Page, MasterPage and UserControl in my project.
So I create basepage class and share some logic, then inherit from System.Web.UI.Page. My problem is I want use single basePage for any type of UI content.

You can not inherit from multiple base classes in .NET, so you can not do what you are looking at here.
However, you probably do not need to do it. Page, MasterPage and UserControl can all access the currently rendering Page and MasterPage instances via properties. You can then test their actual types and cast them appropriately, to gain access to the common code you need.

Related

ASP.NET Master Pages in VB>NET

I have an asp.net website with individual .aspx files which all inherit my Custom Class. There are common features to each .aspx page such as menus and things like that. So I have created a Master Page.
I want to put the common methods which call methods from my custom class in my master page code behind and I cannot. As I don't seem to be able to get my Master Page to inherit from it.
I want to change the following..
Partial Class modules_MyPage
Inherits System.Web.UI.MasterPage
To something like this...
Partial Class modules_MyPage
Inherits MyCustomClass
The above damages my content pages. I was wondering if anyone could advise me on the right approach.
What you want is something like this:
Partial Class modules_MyPage Inherits MyCustomClass
where
Partial Class MyCustomClass Inherits System.Web.UI.MasterPage
but this isn't possible if your class already inherit from web.ui.Page
really usefull article

Base Class to use with ASP.Net Page and UserControls

Is it possible to create a base class that can be inherited from an Asp.Net Page and a UserControl. I know that Page inherits from System.Web.UI.Page and controls inherit from System.Web.UI.UserControl so I couldn't come up with a base class that could be used for sharing common code (instead of repeating same code in several spots).
True multiple inheritance is not supported in .NET. But you could create a base page class and place common code there. Then create a base class for controls which would have a (protected) property that would cast current page to the base page. This gives access from within a user control to the public properties of the base page class.

inheriting the .aspx pages?

Can we inherit the aspx page into another aspx page. If yes how can we do that, Thank you.
I think there are two things you should consider here::
1.Create a new base page type, and have your codebehind classes inherit from that, e.g.:
public abstract class MyPageBase : System.Web.UI.Page
{
// Implement custom shared logic here.
}
2.Move some of your page control logic into partial controls that you can inject into other pages.
I don't think so. You can however, create a master page and use it in the pages you want. That is the way "inheritance" of aspx pages works.
Master page tutorials:
http://www.asp.net/master-pages/tutorials
You can inherit the code (i.e. the classes derived from System.Web.UI.Page), but you cannot inherit the markup.
For markup "inheritance", use ASP.Net mechanisms such as User Controls (ascx) or MasterPages, as other responses suggested, or create controls dynamically.
I am not entirely sure that I understand your question correctly, but you surely can inherit a ASPX page. In your code behind file for a page you have a class declaration where the page inherits from System.Web.UI.Page. If you want to use a different base page, you could simply make another base class that inherits from System.Web.UI.Page and change the code behind defined classes inherit from you new base page.
The problem will of course be to make an appropriate layout shared elements and if that is your main concern you are probably better of using master pages.

Are ASP.net Content Web Forms able to access variables declared in the Code Behind section of it's Master Page?

I have a Master Page which controls the styling of my site. In the Code Behind, there are a few class instances instantiated as well as variables. These classes validate user access and then create user objects
I have a few Web Content Forms which carries out instructions based on the user objects. So far it seems that on each Web Content Form I have to create new instances of the classes found on the Master Page. This is doubling my work for every Web Content Form.
Is there anyway I can inhereit Classes and objects instantiated in the Master Page Code Behind?
Expose the objects (and even controls) as public properties (get only for controls) on the Master page. Then, in each aspx page you want access to these objects, add the following declaration at the top:
<%# MasterType VirtualPath="~/MyMasterPage.master" %>
As #Kristof points out, simply access your properties like Master.PropertyName
Also, you can determine if it makes sense to store the objects in the users Session (don't forget that they must be serializable if you use DB for session state). I do this often and control access to them via properties in a base Page class that all my pages inherit from. Actually, I have a base master, page, and usercontrol so I have access to the same properties (for me it's CurrentUser) everywhere.
I believe you can if you make the properties public.
Then in your child-page you can make the call something like this:
SiteMaster master = (SiteMaster)this.Master;
master.MyProperty = 0;
Where SiteMaster is the class for your master page. (SiteMaster is the default for the app templates)
Though my mind can deceive me, I haven't done it for a while...

Using SavePageStateToPersistenceMedium() for Master Page ASP.NET

Please refer to the topic http://www.codeproject.com/KB/viewstate/SaveViewState.aspx. The topic demonstrates how you can save ViewState to a file system over server so as to make ViewState very small on roundtrips. The author had created a class BasePage by inheriting System.Web.UI.Page and all the pages are derived from this class.
The site I am developing uses a masterpage and all the pages are derived from this masterpage. When I try to override SavePageStateToPersistenceMedium(), a compilation error is generated indicating that there is no such method to override within System.Web.UI.MasterPage.
How could I solve this problem?
I have found the solution. Actually the aspx page is derived from System.Web.UI.Page while the masterpage is derived from Control class. There the method SavePageStateToPersistenceMedium() is available within aspx page only not in master page. You have to override this method within each aspx page or create your own base class derived from Page class and then override the method.
Using a PageStatePersister override would be an easier way to change the ViewState persistance mechanism on all pages without requiring a base class.
Milan Negovan has written a good blog post on the different options using the PageStatePersister, with some additional links.

Resources