Masterpage and Content pages inheriting from a single class - asp.net

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.

Related

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.

Best way to set master page properties in content page in asp.net?

There are different ways to set the title for content pages from Master page
by findcontrol
by creating property in master page and setting the value in content page
As both method requires an object creation of master page which will be little heavy
myMasterPage myMaster = (myMasterPage)this.Master;
so I have tried it by creating a class and it worked -
public class clsmaster
{
public static clsmaster objmaster = new clsmaster();
public strtitle {get;set;}
}
Now I just need to access this static object and set the property in the content page and in the master page I just need the controls to take the value from this class (clsmaster).
I would like to know which one is the better approach and why with description please?
I generally advise creating a BasePage class of some sort that encapsulates the behavior you want through all of your pages, these objects are assumed to always have the same master page, if you need other setups then you can create other objects as necessary.
From there you can create some properties or methods to allow the BasePage objects to access the master page or its associated properties in a very easy to code way such as this.Title = "MyTitle"
You can get fancier and create some virtual methods on your BasePage class that you can then override on the physical pages as necessary, to set titles, etc, as needed, without ever grabbing the Master page object directly (which yes, is annoying and ugly). This might look something like this.Title = GetTitle(); (GetTitle is a virtual method on the BasePage that is overridden in the child pages as needed for those pages you want to set a title for).
This makes strong use of inheritance and lets you add functionality to all of your pages very easily, it may be overkill for what your doing but I've never found a situation that was too simplistic for this architecture, it just works really well. I personally find this design far better than constantly using FindControl(), which tends to be error-prone when control ID's change, etc.
FindControl() is bad because if the control ID's change, then you might forget to update them in the FindControl reference, and now it'll break the next time its executed, I stay well away from static stuff like this if at all possible for this very reason, it's a cheap, quick but error-prone solution.
Accessing the Master page directly isn't inherently bad, I just can't stand stuff like:
myMasterPage myMaster = (myMasterPage)this.Master
Gets old, is uglier than it needs to be, wrap it in an accessor property at the very least ;)
I like the following better:
Title = "My Title"; // Property
or
Title = GetTitle(); // Virtual method

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.

Access data on Page from Web Control

I created a web control, and it needs some data from its parent page.
How can it access that data?
Update: Thank you for the quick solutions, however they don't work for me. Visual Studio doesn't recognize the name of the page as a class. I took the name from where the class is defind:
public partial class Apps_Site_Templates_CollegesMain : cUICommonFeatures
(cUICommonFeatures inherits from System.Web.UI.Page)
But in the control, when I define
protected System.Web.UI.Page parentPage;
parentPage = (Apps_Site_Templates_CollegesMain)Page;
I get a compliation error:
The type or namespace name 'Apps_Site_Templates_CollegesMain' could not be found (are you missing a using directive or an assembly reference?)
I feel like I'm missing something really basic here, and I'll probably be very embarrassed when I get an answer, but I do need help....
If the parent page class is named lets say ParentPage, you can do this within the control:
ParentPage page = (ParentPage)this.Page;
Then you can access the properties and methods on ParentPage. If you have more pages using the same control, you should use an interface on the parent page to access the properties on the page.
IParentPage page = (IParentPage)this.Page;
Does that answer your question?
Controls should be written to be independant of what page they're on. If the control needs a piece of data, then it should expose a public property which is of the type of the data that it needs. The page it is on wo uld then set that property to the data that the control needs. This permits the control to be used on another page, or even made part of a UserControl and that UserControl then used on the parent page.
There are many ways to do that. I think the best one is adding a property for the data control needs and in your page set this property.
Also you can access to your page from your control like that :
string dataYouWant = ((YourParentPageName)Page).GetData();
Or you can add the data to viewstate, and read it from the child controls.
But as I said, I would choose the first one.
All Controls (server controls, usercontrols and custom controls) expose a property Page which allows you to access the containing Page instance from the code of the control.
Therefore, you could simply do:
// In Usercontrol code:
MyParentPage parentPage = this.Page as MyParentPage;
if (parentPage != null)
{
// Access the properties of the Parent page.
string t = parentPage.Title;
}
The article "Mastering Page-UserControl Communication" offers a good beginners introduction to control-Page interactions.

Programmatically define a nested masterpage's master, Is it possible?

Currently I have a site that is set up using a masterpage and a nested master page. The master page setups up the header and footer info. The nested masterpage is used once logged into the site.
The issue I have is that I want to programmatically load a different masterpage to define different header and foot info.
I don't think I can use the OnPreInit() in each content class to set a different masterpage. I don't think I can do this because each Content page uses the Nested Masterpage.
What I would like to do is programmatically set which masterpage is called in the NestedMaster.
Any ideas?
I saw this blog posted on another MasterPage question. Before trying this route I wanted to see if anyone else has experienced this.
Thanks
EDIT:
In the CS page:
public class AdminBasePage : BasePage
{
protected override void SetMasterPageFile()
{
Page.Master.MasterPageFile = "~/PathToMaster/Site.Master";
}
}
Look here:
Nested Master Pages
Page.Master.MasterPageFile = "~/PathToMaster/Site.Master";
(Thanks for the answer Ken and Brad, however I read most of that long tutorial before spotting the simple one line of code had been added to your question, so am making it more obvious by adding it as an answer here and will edit your post. Cheers)

Resources