How to Inherit a Page into a Class in ASP.Net? - 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

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.

What's the best method for extending the functionality of a standard Web Server Control in ASP.NET?

Currently my code resembles this:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="CustomPanel.ascx.cs"
CodeFile="CustomPanel.ascx.cs" CodeFileBaseClass="System.Web.UI.WebControls.Panel"
Inherits="MyProject.CustomPanel" %>
...
namespace MyProject
{ public partial class CustomPanel : System.Web.UI.WebControls.Panel
{ ... }
}
However, I'm really not sure how to proceed from here, because this does not appear to follow a logical inheritance path that includes System.Web.UI.UserControl
Am I doing something wrong? How can I inherit Panel and extend standard event-handlers, such as PreInit?
Are you wanting to extend or encapsulate a Panel here?
Extending it would be done not with a User Control, but with a normal class file, with the class inheriting from Panel.
Encapsulating it would be done in a User Control, as appears to be what you have done here; In such a case, you put a Panel instance inside the User Control's ascx file... you do not attempt to inherit from it.
It actually looks like you are trying to do both at the same time; you shouldn't do that. There is no direct inheritance path as you seem to be looking for that contains both Usercontrol and Panel; you'll have to choose one of the above options or the other.
To extend it, you should create a new class, which inherits the System.Web.UI.WebControls.Panel class. Once you have created your custom class, you can override existing methods from the Panel class, which are marked as virtual. (Typing the keyword override in Visual Studio, while inside your class, will pop up a list of virtual methods)

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