Base Class to use with ASP.Net Page and UserControls - asp.net

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.

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

How to Inherit a Page into a Class in ASP.Net?

I have created a class in asp.net and I want to inherit the Main Page. I have tried below but it has an error:
Public Class Class1
Inherits Main Page
End Class
In windows Application inheriting form is much easier than in asp.net. Just like below:
Public Class Class1
Inherits FormMain
End Class
Can someone help me on inheriting page in a class?
Thanks in advance.
Your class name must be a single word. Try Main_Page or MainPage since in many cases the underscore character is hidden by other elements or the cursor.
Otherwise, search you project for the class and make sure you are using the same name as the actual implementation of the class.
This is probably not something you want to really do. The web page in ASP.NET WebForms consists of two parts - Design and Code-behind. The design part is translated into rendering Visual Basic code during compilation.
I can't see a reason for deriving from a WebForms page. What are you trying to achieve? You can certainly change the base class the page uses, but there is practically no logical reason to derive from it.
EDIT:
You might be actually trying to do something in the sense of having a base page and then other pages that have the same basic "structure". That is possible, but using a different ASP.NET mechanism called MasterPages (http://msdn.microsoft.com/en-us/library/wtxbf3hh.ASPX)
Assuming your MainPage is your inheriting class, then make sure it's declared something like:
''' <summary>
''' Base page for all other web pages in the system.
''' </summary>
Public Class MainPage
Inherits Page
...
End Class
Your ASP.NET class would then be:
Public Class Class1
Inherits MainPage
...
End 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.

Inherit class from page, usercontrol, and masterpage

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.

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