Master page inherits tag question - asp.net

Going through the microsoft authentication tutorial listed here they have you create a master page. Upon generation by Visual Studio the first list in the file looks like this:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="FormsAuthentication.Site" %>
The project is called FormAuthentication and the master page is named Site.Master. When running the project I get the error:
Compiler Error Message: CS0426: The type name 'Site' does not exist in the type 'System.Web.Security.FormsAuthentication'
and the line referenced looks like this, in an auto-generated file
Line 133: [TemplateContainer(typeof(FormsAuthentication.Site))]
Removing the "Inherits='FormsAuthentication.Site' " portion of that initial tag resolves the issue but I'm trying to understand what is happening here. What is actually going on here?

Name of your project (and maybe namespaces) conflicts with ASP.NET form authentification class name: System.Web.Security.FormsAuthentication. I think you are missing namespace name or reference.

If you could rename, or add another level of namespace around FormsAUthentication so that it would be like: Custom.FormsAuthentication.Site, that would alleviate the problem.

Related

What is the preferred workaround when an .aspx file's code-behind file is missing?

In the source code for a VB ASP.NET website that I'm trying to get compiling, I've got a couple of "The file 'bla.aspx.vb' does not exist" errors; there is a corresponding .aspx file, but it's missing its companion .aspx.vb code-behind file.
The error msg displays because of this in the .aspx file:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Bla.aspx.vb" Inherits="cms_ShowEscalatorRule" %>
To just get the project compiling (this is just for running the project locally, the code I change won't affect the production code), would it be preferable to:
0) Comment out that section entirely
1) Remove the "CodeFile="Bla.aspx.vb" portion
2) Add a code-behind file
If the last option is best, what needs to be in it for a minimal amount of code - just enough to prevent the "missing file" error?
It's rather macabre to me that there is no contextual menu item for .aspx files that allows a minimal corresponding .aspx.vb to be created.
You may have to have a file because the .aspx may have references (and you will have to recreate those in the code-behind) to items from the code-behind file.
The minimum that's needed is a class inheriting from Page:
public class Bla : System.Web.UI.Page
{
}
This - of course - is not mandatory - you may as well use the alternative approach and inline the code-behind in a server-side script block in the .aspx file. Former approach however is much neater.
G. Stoynev basically had what I needed, but since this is VB, I had to change it to:
Public Class Bla
Inherits System.Web.UI.Page
End Class
I have never used VB before, so I used this to make the translation.

In Web Site, could not load type

There is already a question in stackoverflow where there is an error for a web application the compiler cannot find the load type.
This is not like that at all. This is a Web Site and the problem is in the building of the code when it looks for namespaces and definitions that already exist in the code.
This compilation error points to the first line of .master file and refers to an "Inherits" assignment.
The code behind only shows a warning, not an error. Any suggestions?
Your master page declaration should look like (presuming all files on same level in directory):
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Admin.master.cs"
Inherits="Applications" %>
Your code-behind declaration should look like this:
public partial class Site : System.Web.UI.Page
{
...
}
If you were using VS, then it should have done this automatically for you. Are you sure this is not a Web Application project template?
In any case, your page code-behind should be inheriting from the page type; in your case, you are inheriting from a user control (master page is a user control).

.Net 3.5 NameSpace Problem?

Got an asp.net 3.5 app - compiles fine, but when browse to page I get the error below.
Searches for this all seem to indicate it is a namespace problem, but all of my pages seem to declare the proper namespace.
Any assistance appreciated!
Phil J.
Server Error in '/Internet/bm2/bm2' Application.
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: 'bm2.PolicyLookup' is not allowed here because it does not extend class 'System.Web.UI.Page'.
Source Error:
Line 1: <%# Page Language="vb" AutoEventWireup="false" CodeBehind="PolicyLookup.aspx.vb" Inherits="bm2.PolicyLookup" validateRequest="false" aspcompat="true"%>
Line 2:
Line 3:
Source File: /internet/bm2/bm2/policylookup.aspx Line: 1
=======
The System.Web.UI.Page is indeed inherited from in the code-behind:
Namespace bm2
Public Class PolicyLookup
Inherits System.Web.UI.Page
Protected WithEvents lblResponse As Label
Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
Dim myModel As New bm2.Model
Dim postData
Public objMessage
If this is a namespace problem (very unlikely), you'll either need to add an #import statement to your page:
<%# Import namespace=”My.Namespace.Where.bm2.PolicyLookup.Exists” %>
or modify your #Page directive to include the fully-qualified class name
....inherits="My.Namespace.bm2.PolicyLookup"
Of course, this only works if bm2.PolicyLookup descends from System.Web.UI.Page.
The #Page attribute
inherits="bm2.PolicyLookup"
tells ASP.NET that the class bm2.PolicyLookup inherits from System.Web.UI.Page, and that it should be instantiated when processing the page to handle on_load(), render(), etc events. However, ASP.NET believes that your class bm2.PolicyLookup does not inherit from System.Web.UI.Page, thus the error.
In order for the webforms engine to be able to process your page, it has to descend from the existing Page class. That class provides all of the event handlers, rendering methods etc. that are required.
Also, a bit of unrelated advice: Visual Studio hides a lot of functionality when working with a VB.Net site. Consider moving to C#, and to a web application project instead of a website project.
I don't know if there is a PolicyLookup class in System.Web (I don't think there is), but I ran into a similar problem when I tried to name a page SiteMap.aspx, since there was a type named SiteMap (though it was in a different namespace, it still seemed to cause a problem). The fix was to either rename completely, or have the code behind class name be prefixed with an underscore (you may have to update the Page directive in your aspx to match the new type 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