namespace in ASP.NET 2.0
-
I noticed that there is no namespace in the code file when I created a new ASP.NET project in VS2005. Anyone know why Microsoft make this change, pls help me (I love links :D) Thx all!
-
I noticed that there is no namespace in the code file when I created a new ASP.NET project in VS2005. Anyone know why Microsoft make this change, pls help me (I love links :D) Thx all!
Hi there. If you mean that you don't see the
using
orimports
statements for referencing namespaces, it may be that they are already established in the project properties as defaults for all forms to use. -
Hi there. If you mean that you don't see the
using
orimports
statements for referencing namespaces, it may be that they are already established in the project properties as defaults for all forms to use.this is VS.NET 2003 code generation for a page:
.... namespace WebApplication1 { class Class1 { ... } }
and the VS2005 version... partial class Class1 { ... }
no namespace (WebApplication1), thats what i mean :D, thx for reply -
I noticed that there is no namespace in the code file when I created a new ASP.NET project in VS2005. Anyone know why Microsoft make this change, pls help me (I love links :D) Thx all!
Hi Nam, Because of the changes in the ASP.NET compiling model and also in the web page code behind model, so by design the class in code-behind is put in the default namespace. Remember that this is partial (incomplete) class, and at runtime the ASP.NET will associate another partial class (normally put in the default namespace
ASP
) with the partial class in code-bebind to create a complete class to represent the web page. However, you can put the partial class in code-behind if you want, but you need to remember to update the Inherits attribute of the Page directive accordingly. For more information, you should see the documetation on the ASP.NET compiling model and the web page model in MSDN. -
Hi Nam, Because of the changes in the ASP.NET compiling model and also in the web page code behind model, so by design the class in code-behind is put in the default namespace. Remember that this is partial (incomplete) class, and at runtime the ASP.NET will associate another partial class (normally put in the default namespace
ASP
) with the partial class in code-bebind to create a complete class to represent the web page. However, you can put the partial class in code-behind if you want, but you need to remember to update the Inherits attribute of the Page directive accordingly. For more information, you should see the documetation on the ASP.NET compiling model and the web page model in MSDN.How to include the generated partial class to the main code-behind class as you said? Is there and option in the IDE to do that? I dont think that abandoning the namespace in the code-behind file is a good idea :( . Could you give me the links to find out more? I cant find them clearly in MSDN. Thx for reply.
-
How to include the generated partial class to the main code-behind class as you said? Is there and option in the IDE to do that? I dont think that abandoning the namespace in the code-behind file is a good idea :( . Could you give me the links to find out more? I cant find them clearly in MSDN. Thx for reply.
Nam, You can put the partial class in code-behind in your custom namespace as you would with the ASP.NET 1.1:
namespace MyNameSpace
{
public partial class Class1 : System.Web.UI.Page
{
....
}
}And you will do this in the code editor, also remember to update the
Inherits
attribute accordingly:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="..." Inherits="MyNameSpace.Class1" %>
So basically, you would not need to worry about the code to declare all the server controls placed in the web page .aspx since they are placed in another dynamic partial class generated by the ASP.NET. The idea behind this is to seperate all automatically generated code in a partial class so that the code in your code-behind is really neat and clean, and you can freely access the controls in code-behind.
Duong Tien Nam wrote:
I dont think that abandoning the namespace in the code-behind file is a good idea
Unfortunately, I haven't seen a way using the IDE to set the namespace for the partial class by default, that means you would need to use the code edtior. And I guess that you might find it difficult or strange to you, but in fact, there are some big changes in the ASP.NET compiling model over the ASP.NET 1.1. In old version of the ASP.NET, you basically have a single output assembly whose name is normally the namespace declared in code-behind. At runtime, this assembly is put in the bind folder, and with this model you will have some disadvantages like updating the assembly. However, you should be much more flexible with the new compiling model introduced in the ASP.NET 2.0. You can find more information from the links: ASP.NET Compilation Overview [^] ASP.NET Web Page Code Model [^] http://weblogs.asp.net/scottgu/archive/2005/08/21/423201.aspx[^] You can certainly find more re