As a general rule, I like to keep my applications minimal, so that every file and every piece of code has a place and a purpose in my mind. Whenever I created a web project in VS.NET 2003 the first thing I would do was delete the global.asax and its code behind, delete the AssemblyInfo.cs file (since it rarely makes sense to version your code behind assembly), open the web.config file and reduce it to a single self-terminating element <configuration />, and finally rename the generated WebForm1.aspx file to Default.aspx, fixing up the corresponding code behind to match.
That's why I often tell people that one of my favorite features of VS.NET 2005 is the "Empty Web Site" template, which generates nothing but a directory for you, leaving a lovely white canvas on which to paint your application. Along these lines, whenever I add a new WebForm to a site, I typically leave the "Place code in separate file" checkbox unchecked, since I find more often than not that my pages don't require any supplemental code at all - most of my code ends up in a data access layer or set of utility classes that I reference declaratively through object data sources or in data binding expressions.
On the occasions where I do find I need supplemental code for a page, however, I do like the separation that code behind provides and generally prefer that over embedding code directly in a runat="server" script block in the .aspx file itself. Unfortunately, what this means is that I must retrofit the page to use code behind with the following steps (assuming the page in question is named MyPage.aspx):
- Add a new class to the site (File | New File, choose Class template, Name=MyPage.cs, be sure to respond 'No' when prompted to place the file in the 'App_Code' folder.
- Modify generated class to be partial and to inherit from Page.
public partial class MyPage : Page
{
...
- Modify the @Page directive in MyPage.aspx to reference the new code behind file.
<%@ Page Language="C#" Inherits="MyPage" CodeFile="MyPage.cs" %>
- Remove the <script runat="server"> </script> block from MyPage.aspx.
What I'd really like to be able to do is right-click on a .aspx file with no associated code behind file in my Solution Explorer and say 'Add code behind'. Plugin anyone?
 |
Limited training time? Need to learn ASP.NET 2.0 and SQL Server 2005?
|
Posted
Mar 21 2006, 07:56 AM
by
fritz-onion