ASP.NET Master Pages in VB>NET - asp.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

Related

Master page inheritence dilemma

Bit of a confusion for me here. This is ASP.NET 4.0. Looking at the markup, it appears that Default.aspx inherits from Site.Master page. But looking at the class definition of Default.aspx (which is named _Default), it inherits from Page class and not SiteMaster.
Now I need to share a few functions across multiple inherited pages and was looking to add them to SiteMaster class so that they would be available in all inherited pages. Can I use SiteMaster class for my purpose, or should I add an independent module to my project and add all my functions to that?
Figured it out. For anyone else looking for a solution, all you need to do is to add the following directive to your markup:
<%# MasterType VirtualPath="~/Site.Master" %>
You can add this line to the top of ASPX file, just under the <%# Page ... %> line. Once added, simply rebuild your project. Now all the public members of your master page class will become available through the strongly-typed member named Master. For example, you can now use the following code to access a public function called MyFunc() defined in the master page class:
this.Master.MyFunc(); //C#
Me.Master.MyFunc() 'VB.NET
MasterType directive actually adds a shadowing member named Master that is strongly-typed as SiteMaster class (or whatever master page class you have specified) when spitting the code-behind class. This new member hides the base class member (whose type is the generic MasterPage class) and thus allows you to access your master page members directly. The property is defined in the .aspx.Designer.cs/vb file like this:
Public Shadows ReadOnly Property Master() As YourProjectNamespace.SiteMaster
Get
Return CType(MyBase.Master, TagTrakkerOnline2.SiteMaster)
End Get
End Property

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.

Mapping a .NET Class to a custom page extension

What I would like to have is a mapping between a custom file extension to a class that is not System.Web.UI.Page but a class of mine that inherits from System.Web.UI.Page.
i.e.:
*.aspx -> System.Web.UI.Page
*.my -> My.Package.MyClass (inherits from System.Web.UI.Page)
I know I can map any extension to be treated like .aspx but I can't find the way to do what I have in mind.
Thanks
d
You need a ASP.Net HTTPHandler for this.
I know you mentioned that you already inherit from System.Web.UI.Page but also look into the lighter weight ASHX Handler
A custom page handler, but also any ASP.NET Page can inherit from your custom base class, so you wouldn't necessarily need to do a handler. You could just have the code-behind pages all inherit from My.Package.MyClass instead.
I think I was trying to build a castle when I just needed a hut.
When I'm in case of a .aspx page without a separate code file I just have to add the Inherits directive, making it pointing to my custom class, like this:
<%# Page Language="C#" Inherits="MyPackage.MyClass" %>

Resources