Designer.cs file creates errors automatically - asp.net

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.

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.

Master page inherits tag question

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.

Confusion with Root Namespace in Asp.net web project

I have an VS2008 solution with 2 projects, WebUI and Domain; WebUI references domain
The Root Namespace in the WebUI project is:MyCompany.MyProjectName.WebUI
The Root Namespace in the Domain project is blank. (Namespaces are manually declared for all classes)
So everything has been working fine, until I tried to reference a class in Domain via a fully qualified path:
MyCompany.MyProjectName.Domain.EntityClasses.SomeClassName
VS was not able to find the class.
However, if I do at the top of the code file:
Imports MyCompany.MyProjectName.Domain.EntityClasses
....then it can see it. (Just as SomeClassname, not the fully qualified name)
Does this make any sense?
So, then I cleared my WebUI Root Namespace, and voila, the fully qualified declaration then does work. However, doing this then seemed to invalidate the registration of all my user controls on my pages. The only thing that seemed to solve this was in the codebehind of each user control, manually add a namespace of MyCompany.MyProjectName.WebUI
...which might make sense as perhaps the namespaces of the pages somehow had still retained the root namespace value. But, if I was to create a brand new aspx page and drop a user control on (this is before manually adding the namespace), even that page couldn't properly register it. Yet, the uwer control properly rendered in design view....so the VS UI seemed to be able to properly resolve it, but the compiler seemingly can't.
So from what I can tell, I can at least get things to work by manually wrapping user controls in the proper namespace. Why this is necessary for aspx pages, that have no namespace specified, to see the user controls, seems to make no sense.
Is there some fundamental principle I am misunderstanding??
Ok, I think this is the problem:
When you have a root namespace specified for your project and create a new aspx page, the aspx page will hardcode the root namespace in its definition at creation time, ie:
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/MasterPages/DefaultMasterPage.Master" CodeBehind="Home.aspx.vb" Inherits="yourCompany.yourApp.WebUI.Home" %>
...and that will never change even if you change the project root namespace. If you didn't have a root name space when the page was created, you'd get:
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/MasterPages/DefaultMasterPage.Master" CodeBehind="Home.aspx.vb" Inherits=".Home"
So if you change the root namespace half way through, all the prior aspx pages will have their namespace hardcoded to the old value. However, I think the code behind for the aspx does react to the removal of the root namespace, so you start to get weird behaviour with user controls, etc. and you have to go into all the code behinds and explicitly set a matching namespace in there for the pages to work properly. In this case, I think the best thing to do is search all your aspx files for this explicit definition of the namespace (in the Inherits tag) and remove it. You should, I think, still be able to explicitly set the namespace in the codebehind if you so choose, which the aspx will inherit. Not 100% sure though.
Long story short, decide which namespace naming method you want at the start and stick with it.
I would suggest you use the Web Application project format to avoid this kind of thing. Web Site projects are a retrograde move for anything other than simple sites.

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?

Resources