Should ASPX code-behind be within a namespace? - asp.net

Something like this:
Namespace Test_NS
Partial Class Test_WebSite
Inherits System.Web.UI.Page
End Class
End Namespace
Or should I leave it alone and only keep the classes in my App_Code under a Namespace?

It makes little difference for a project. By default, your code-behind is NOT in a shared name-space. Also, your classes (I assume you're still talking code-behind) are not stored in the app_code folder.
Do you use visual studio? It will do the organizing for you, and if you're not sure, then I'd just leave everything as-is.
Any new .vb files (not code-behind) that you create independently do go in the app_code folder, and you can group these in name-spaces (or not) as you wish.

Related

Designer page is prepending Global to custom controls definition namespace

I've got a custom control defined in my App_Code folder. When I add it to a page, I get the error Type 'Global.XXX.xxx.MyControl' is not defined
Class:
Namespace XXX.xxx
Public Class MyControl
Inherits TextBox
...code...
End Class
End Namespace
Page Useage:
<%# Register TagPrefix="xxx" Namespace="XXX.xxx" %>
...
<xxx:MyControl runat="server" id="StartTime" />
Designer:
Protected WithEvents StartTime As Global.XXX.xxx.MyControl
So what's happening is Global is being prepended to the namespace in the designer file. For some reason, the IDE thinks my custom control is living in the global namespace, but as far as I can see from searching the Object Browser, it isn't. Manually going in and deleting the prepended Global from the designer file allows me to build the page successfully, view it and test the functionality of my control, however this is far from an ideal solution, as the designer file is regenerated every time you make a change to the corresponding page.
Any ideas as to why it thinks my custom control, living in the App_Code folder, is in the Global namespace? Any suggestions on a better fix than the manual one?
EDIT: Perhaps a better question would be how to get that namespace into the Global namespace, so that when it creates the designer file, it's correct?
The question Type or namespace could not be found from App_code folder may be what you want.
Either put your class in a different folder or change the build settings as per http://vishaljoshi.blogspot.co.uk/2009/07/appcode-folder-doesnt-work-with-web.html

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.

partial class naming for asp.net web form default template

My company has visual studio asp.net web site projects. When adding a new web form using the default webform template it will automatically create the codefile with the partial class name using the path and the filename you selected. For example, if you added a new web form named Default.aspx to the path User/Feature/ the partial class name will be User_Feature_Default. This works fine. The issue (which may not be an issue) is that of course if you move those files around it will not update the partial class name. Makes sense. Visual Studio is only going to do so much tweaking. My question is whether or not our development group should update these partial class names when the files are moved into new folders mainly for consistancy sake? Will this cause any other problems? I can't think of any.
If there is a possibility of adding new .aspx pages with the same name in those locations, I would advocate renaming. I've run into issues in the past where I had Copy+Pasted a file to have a template to start from. The file name changes, but the class name and .aspx Inherits attribute remain the same.
Because the code-behind pages are partial classes, it will happily let you add multiple classes. But I think those then end up being compiled into a single page class which can spell problems if you have Load/Init logic that should only fire on one of the pages, or if you have functions named the same thing.

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