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" %>
Related
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
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
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.
Given the following statement,
<%# Page Language="C#" AutoEventWireup="true" CodeFile="XXX.aspx.cs" Inherits="XXX" %>
What is the meaning of AutoEventWireup?
What if the value of AutoEventWireup is equal to false
What is the meaning of XXX in the Inherits attribute?
I cannot find the definition of XXX in the auto-created file in ASP.NET 2008. Where is the XXX defined?
Thank you
AutoEventWireup = false means that your Page_Load event will not be automatically hooked to the page's Load event and so on for PreRender and the other page lifecycle events. It means in the constructor of your code-behind base class for the Page, you will have to manually do
Load += new ..EventHandler(Page_Load) etc
Inherits tells the page which class is the base class for the class that the runtime will generate when your application starts up. The auto-generated class will be in the ASP namespace and be put in the Temporary ASP.NET Files and will inherit from your class. This is how protected properties and event declarations in your code-behind can actually serve as handlers that are specified in the declarative .aspx markup
XXX is usually side-by-side right next to the aspx file and is the same name as the aspx file, unless it is Default, in which case that is a C# keyword, so sometimes it uses _default as the class name while the page itself is Default.aspx.
You should probably try to read some tutorials on ASP.NET page inheritance, here is an example, but you should search for more:
http://west-wind.com/weblog/posts/3016.aspx
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.