inheriting the .aspx pages? - asp.net

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.

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

page method in base page or master page

Where's the best place to put a page method so that it's available in every page? The page method is called from the client with a jquery $.ajax{...} function: it sends and receives json objects.
Should I put this page method in a base page or in the master page?
Thanks.
It is more consistent to put such logic in a class that derives from System.Web.UI.Page. Every page that should have that logic should derive from your custom class.
The MasterPage should be used for containing logic relevant to the UI.
Also consider that you may add a page in the future, that should share the same design like the rest of the pages, but not the logic. In that case you would simply derive that page from System.Web.UI.page and use the MasterPage for layout purposes. If you had chosen the MasterPage, you'd either have a page that contains logic it should not, or you'd have to do a rewrite

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.

Masterpage and Content pages inheriting from a single class

I am trying to create a general class, in which all my ASP.Net pages inherit from so I can share functions across multiple pages.
To do this I would create a new class which inherits from System.Web.UI.Page (the content pages need to inherit this), and then my content pages would inherit the newly create class.
My problem is that the Masterpage inherits from System.Web.UI.Masterpage.
How can I set up my project so both content pages and Masterpage and use functions from the general class?
Please don't hesitate to ask if I am unclear!
Thanks!
E
First, not sure why you'd want to do this. By their function Master Pages should mostly have functions that your Pages shouldn't be concerned with and visa versa. And if you just need some common functionality that isn't page dependent you can just create a static class (much like Math) or a helper class of some kind that you can implement in MasterPage and Page custom base classes.
But your only real option is to create two custom base classes. One that inherits MasterPage and the other from Page. Both will need to implement an interface ICommon which you create. Then create another static class that you can proxy all the functions to.
Yucky solution but it's the only one I can think of.
EDIT
Here's a better solution
public class Helper
{
public static int getUserID(...)
{
// ... Code to get User ID
}
}
In your masterpages and pages use
int UserID = Helper.getUserID(...);
I don't think you can do this, the MasterPage inherits UserControl and Page inherits TemplateControl. Like Spencer said, I would just create a Helper/Utility class.
I know It's too late, but just want to share some one might come across the same.
This is a sample code using Extension functions:
public static void Commonfunction(this TemplateControl ctl)
{
// Your Code here
}
Call this function as
this.Commonfunction();
in any Page or MasterPage.
Although the question is 5 years old, I wanted to share another option for future readers.
From what you explained, I suppose that what you call MasterPage is like a frame which loads the content page on a side of the screen, for example.
If I'm right, you can create a BaseMasterPage class with your functionality and then create a MasterPage for your content pages (which you could also use to place some of the code from your current pages). Next step is obviously to make both your current MasterPage and, let's say, ContentMasterPage inherit from this BaseMasterPage.
So this way you can end up having, for example, a LoggedUser property in both your frame page and content pages.

Resources