Website:
The Web Site project is compiled on the fly. You end up with a lot more DLL files, which can be a pain. It also gives problems when you have pages or controls in one directory that need to reference pages and controls in another directory since the other directory may not be compiled into the code yet. Another problem can be in publishing.
If Visual Studio isn't told to re-use the same names constantly, it will come up with new names for the DLL files generated by pages all the time. That can lead to having several close copies of DLL files containing the same class name,
which will generate plenty of errors. The Web Site project was introduced with Visual Studio 2005, but it has turned out not to be popular.
Web Application:
The Web Application Project was created as an add-in and now exists as part
of SP 1 for Visual Studio 2005. The main differences are the Web Application Project
was designed to work similarly to the Web projects that shipped with Visual Studio 2003. It will compile the application into a single DLL file at build
time. To update the project, it must be recompiled and the DLL file
published for changes to occur.
Another nice feature of the Web Application
project is it's much easier to exclude files from the project view. In the
Web Site project, each file that you exclude is renamed with an excluded
keyword in the filename. In the Web Application Project, the project just
keeps track of which files to include/exclude from the project view without
renaming them, making things much tidier.
Reference
The article ASP.NET 2.0 - Web Site vs Web Application project also gives reasons on why to use one and not the other. Here is an excerpt of it:
- You need to migrate large Visual Studio .NET 2003 applications to VS
2005? use the Web Application project.
- You want to open and edit any directory as a Web project without
creating a project file? use Web Site
project.
- You need to add pre-build and post-build steps during compilation?
use Web Application project.
- You need to build a Web application using multiple Web
projects? use the Web Application project.
- You want to generate one assembly for each page? use the Web Site project.
- You prefer dynamic compilation and working on pages without building
entire site on each page view? use Web
Site project.
- You prefer single-page code model to code-behind model? use Web Site
project.
Web Application Projects versus Web Site Projects (MSDN) explains the differences between the web site and web application projects. Also, it discusses the configuration to be made in Visual Studio.
I think I should clarify how the compilation model works. First of all, you can write everything (including C# code) in a single .ascx
file and inherit from whatever class you want to (that ultimately inherits from UserControl
) by specifying Inherits
attribute (or you might want to omit it) and without a ClassName
attribute. If you do this, you can still access the properties from the .aspx
file that uses this control. The thing is, you can't use the class in the codebehind file of .aspx
page (if it has one) directly. By specifying the ClassName
, if you are using the Website project model of Visual Studio, you can access it from the .aspx.cs
file too. Note that this would not work in Web application project model as every .cs
file will be compiled ahead of the time (prior to the .ascx
file). In that case, even ClassName
attribute wouldn't help much.
In any case, strictly none of the attributes are necessary. You can always use the properties defined anywhere in .ascx
or .ascx.cs
file included in a page in the .aspx
file (but not always .aspx.cs
file).
Edit to address the update to the question:
A) From your source code, it seems you are using the Website model here. Note that I mentioned you cannot use the class directly. I agree this statement might have been a little misleading. What I wanted to note is that without a ClassName
, ASP.NET will choose a name for your user control class but that name is not guaranteed. While you can use the generated name, it's not recommended at all. You should treat it pretty much like an anonymous type where you can use an instance but cannot mention a name. In your example, you are basically referencing an instance (which is constructed on the .aspx
markup), not the class, which is OK.
B) What you are saying is correct. Whatever you declare in ascx.cs
in a Web application will be visible to .aspx.cs
and .aspx
. What I was talking about is properties that you declare in .ascx
in a <script runat="server">
tag. Those will not be visible to .aspx.cs
since it's compiled beforehand.
C) ASP.NET will generate the class defined by the ClassName
attribute in the ASP
namespace. You should use ASP.Some_Name
instead. There is one thing I forgot to mention: in this case, you should somehow reference the .ascx
in the .aspx
markup; either with a Reference
directive (<%@ Reference Control="MyControl.ascx" %>
) directive or with a Register
directive (<%@ Register TagPrefix="abc" TagName="xyz" Src="MyControl.ascx" %>
). This will ensure that the ASP.NET build engine puts the generated .ascx
class in the same assembly as .aspx
page.
Best Answer
What does the Search form need from the inherited custom Page class? You could design your search form so that it is nothing but a form of inputs and public properties that expose the input values. It's hard to say what the best answer is without more information about what value the custom Page class provides. Typically, controls won't need to know much about their parent page.