How can I create a base MasterPage class in ASP.NET? - asp.net

I'm trying to create a base class for a MasterPage in ASP.NET, so that the inheritance structure is as follows:
System.Web.UI.MasterPage -> BaseMasterPage -> SiteMasterPage
BaseMasterPage will simply provide some methods and properties that would be the same across several master pages on the site, so I think having a base class here makes sense.
Sources here and here suggest that what I'm trying to do is possible with subclasses of the Page class, but I can't seem to get it working and I'm wondering if there's something about MasterPage that prevents me from doing this.
Here's what I've got:
Base.Master.vb (In App_Code folder)
Public Class BaseMasterPage
Inherits System.Web.UI.MasterPage
End Class
Site.Master.vb (in a folder called Master)
Public Class SiteMasterPage
Inherits GSHolidays.BaseMasterPage
End Class
First line of Site.master (also in Master)
<%# Master Language="VB" AutoEventWireup="true" CodeFile="Site.master.vb" CodeFileBaseClass="GSHolidays.BaseMasterPage" Inherits="GSHolidays.SiteMasterPage" %>
I've modified the class names, as well as removed the content from the classes for simplicity. The GSHolidays namespace is the default defined in the project file.
Here's the error I'm getting:
In the browser (when navigating to a page that uses SiteMasterPage):
A bit of Googling on this issue lead me to add the CodeFileBaseClass property (I originally did not have it included), which doesn't seem to have solved the issue, but has added another one, inside of visual studio -
In Visual Studio 2017:
I can't seem to find any information online regarding this error, and regardless of what I do it doesn't seem to go away (unless I remove the CodeFileBaseClass property.
I'm really not sure where to go from here, so I was hoping one of you guys could help me out. I apologise that this question is quite long, though hopefully the issue is something simple and someone is able to point me in the right direction. Thanks in advance!

Related

ASP.NET CodeFile, CodeBehind and Inherits

My confusion is not new here or arround the web, yet, i have some questions for which i did not find answers anywhere:
The first question is:
Why is Inherits necessary on CodeFile and not on CodeBehind?
I read: http://msdn.microsoft.com/en-us/library/vstudio/ms178138(v=vs.100).aspx and some more pages, and i understand that CodeFile is for source code and for compilation on the fly while the other is for an assembly. This raised me another question:
Why do everyone say that CodeBehind must be an assembly if i find
File.aspx.cs everywhere including in a test project i have and it
works like a charm? Is this a compiled assembly?
But, as of the first question, and based on the url i supplied, i might understand, why the Inherits is necessary. I assume the compiler must know what is the name of the partial class to compile later. Makes sense. Yet, shouldn't it be necessary on CodeBehind as well? If we are going to merge the partial classes, i suppose i should give the name of the one i want to merge. I even tried adding another partial class to the .cs file and it compiled/ran well.
Am I missing something here?
I also read that CodeBehind is not used anymore and it's CodeFile, the new one.
Any .NET guru to help me?
Thank you all in advance.
UPDATED:
I tried to implement events using no Inherits on CodeBehind. It didn't work. This makes more sense. But, it doesn't complain on compilation, while CodeFile does. Is there any reason for this?
Inherits property carry the class name which written inside the code behind file. you can have multiple classes in same code behind file and inherit in two different .aspx file
CodeBehind file provides the code behind for the aspx page. When you add a new "ASP.NET webpage with CodeBehind", let's call it NewPage.aspx, 2 files are also created, called NewPage.aspx.cs and NewPage.aspx.designer.cs. NewPage.aspx.cs is your CodeBehind file. As Vijay stated, since a (C#) file can have multiple classes (which is different from Java, where a class file can only contain 1 class, and the class name have to be matching the file name), Inherits=[namespace].[class] indicates which class you want to actually use for the web page.
The CodeBehind attribute is for Web application projects. The CodeFile attribute, on the other hand, is for Web site projects.

ASP.NET Error About "Inherits" attribute, even though it's correct?

Having a really strange issue at the moment with an ASP.NET site I'm currently building.
At the moment, I'm writting the HTML and CSS for a page called Dashboard.aspx. But when I go to view the page in a browser, half the time it throws up this error:
ASPNET: Make sure that the class
defined in this code file matches the
'inherits' attribute, and that it
extends the correct base class (e.g.
Page or UserControl).
I've checked the Inherits attribute on the .aspx page, and it matches the Partial Class held in the VB code of Dashboard.aspx.vb file.
When I refresh, it will always come up with this error. Then after a minute or two, it will load fine with no problems. I had a quick look on Google and I couldn't find an answer to this issue.
I have also changed the name of the partial class (and the content placeholder on the .aspx file, thinking that may have caused a conflict) and nothing I do seems to fix it.
Could anyone shed any light on what's going on?
Thanks,
Michael
Make sure the inherits property in the
aspx page matches the class definition
in the .cs file. […] the inherits
property and the class must have the
same namespace and classname
(From: Inherit problem in asp.net)
Additionally, make sure no other pages (accidentally) try to inherit a class from the same code behind file.

Ambiguous Reference

In my WAP project, every .aspx's code-behind and designer share the same namespace. For example my Main.Master.cs and Main.designer.cs are both in the OurCompany.Web namespace by default.
When I go to another .aspx page and use the following, I get an "Ambiguous reference" error because it can't decide if I'm talking about my code-behind or designer file of that master page
<%# MasterType TypeName="OurCompany.Web.Main" %>
but by default this is the way VS creates .aspx pages so should I really care?
The designer files are all marked as "partial" classes so they don't get compiled into their own types.
My guess is that you really do have 2 classes called "OurCompany.Web.Main". A tool like Reflector would let you browser your DLLs so you could tell for sure.
This just happened to me, your problem is the JIT compilation creating temporary "copies" of your assemblies in a temp directory.
Make sure every namespace/partial class declaration is "tight", check for incorrect class names, wrong namespaces.
The problem "just went away" for me as well. Recreating or cleaning the solution will probably do it. Wish I could be more helpful but going cleaning up the source, both manually and with the right click menu probably helped.
I'm guessing you have a master page and a web form page with the same name on the code behind class. And this will prevent your site from working correctly (if it works at all).
I'd go through my aspx.cs files and looking for the class name main (find should work here). I bet you will find two files with the name. You will have to change one of them to something else. Just make sure you also change the Inherits in the .aspx page and the .designer.cs class name.

User Controls Not seeing the Day of Light - Doesn't recognize code-behind methods

This is driving me absolutely nuts.
I created a new WAP project in VS 2008. Copied over the files in my Web Site Project. Added any required references. Tried to convert the Web Project to a Web Application using the "Convert to web application".
None of my user controls are able to see methods in their code behind. They don't even see them so I get errors everywhere saying it doesn't know what this or that method is.
Example:
<%=CreateMenu(xxx.WebMenuType.Occasion, "menuShopOccasion", "Occasion") %>;
That is in my Header.ascx
And so it errors out because it has no clue what CreateMenu is!
In my Header.ascx.cs it's there and was being referenced with no problem in my old Web Site Project:
protected string CreateMenu(xxx.WebMenuType menuType, string menuID, string title)
{
...
}
It's probably a namespace problem. Make sure that the Inherits attribute in your <%# Page ... %> declaration refers to the correct path to the code behind file, including the namespace. The designer file must also be in the same namespace as the code behind.
I am not entirely sure this is your problem but....
you may be missing the .designer.cs files. For your example above there would also be a Header.ascx.designer.cs which contains a partial class (Header) which has all the declarations of the controls in the Header.ascx file?

Designer.cs file creates errors automatically

Flawed as I am, I've received some unneeded help in creating errors in the form of Visual Studio 2008 adding incorrect code to a .designer.cs file. I appreciate, it is probably doing this because of my omission or error - but I will use the excuse that I am in actuality a fledgeling ASP.NET developer so I'm still learning.
The relevant parts of the solution exporer look like this:
/Prototypes
/Project01.Master
- Project01.Master.cs
- Project01.Master.designer.cs
/SampleApplication.aspx
- SampleApplication.aspx.cs
- SampleApplication.aspx.designer.cs
I'm not entirely sure which files to include in the question, so I'll try and guess as best I can. Both the .Master.cs and SampleApplication.aspx.cs include themselves within the Project01.Prototypes namespace (though I'm not precious about that, it's something that was auto-added and worked while I didn't need to think about it).
At the top of SampleApplication.aspx is the following (to enable access to some properties that the Master Page in theory, exposes.
<%# Page Language="C#" MasterPageFile="~/Prototypes/Project01.Master" AutoEventWireup="true" CodeBehind="SampleApplication.aspx.cs" Inherits="Project01.Prototypes.SampleApplication" %>
<%# MasterType VirtualPath="~/Prototypes/Project01.Master" %>
Within the SampleApplication.aspx.designer.cs is:
namespace Project01.Prototypes {
public partial class SampleApplication {
public new Project01.Prototypes.Project01 Master {
get {
return ((Project01.Prototypes.Project01)(base.Master));
}
}
}
}
All this results in the error:
The type name 'Prototypes' does not exist in the type 'Project01.Prototypes.Project01'
I can fix this error and get the build going again by getting rid of the 'Project01.Prototypes.' references within the class itself (leaving the namespace). My problem is that this is only a temporary solution as Visual Studio keeps adding it back in - so I guess the question is where is my mistake actually located?
p.s. If it's important, I'm running in Visual Studio 2008 with a ASP.NET MVC Web Application.
The problem is that your top-level namespace and your master page class have the same name. So when the compiler tries to resolve the type, it's looking for a type "Prototypes.Project01" inside your master page class.
I would recommend using the typename property on the mastertype directive instead of virtualpath, but the parser seems to choke when you use a qualified type name there. So as I see it you have two alternatives:
Rename your master page. Using the project name seems like it could cause confusion anyway.
Don't use the mastertype directive and declare the Master property in your codebehind file manually.
Have you tried closing down VS 2008, and then opening your project again?
That normally does it for me.

Resources