Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Back to VSLive! San Francisco Show Daily Home

email article
printer friendly

The New ASP.NET 2.0 Code-Behind Model
by Fritz Onion

February 1, 2005

Note: Fritz Onion is presenting "Introduction to ASP.NET 2.0" at the ASP.NET Workshop, San Francisco, Monday, February 6. This code is from that session.

The dust has finally settled on the new code behind code beside code separation compile with code-behind model in ASP.NET 2.0. Here's an example of what the final code-behind looks like:

<%@ Page Language="C#" CodeFile="MyPage.aspx.cs" 
               Inherits="PS.AspDotNet20.MyPage" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <body>
    <form id="form1" runat="server">
      Enter your name: 
	     <asp:TextBox ID="nameTextBox" runat="server" /><br />
      <asp:Button ID="enterButton" runat="server" 
                       Text="Enter" OnClick="enterButton_Click" />
      <br />
      <asp:Label ID="messageLabel" runat="server" />
    </form>
  </body>
</html>

The corresponding MyPage.aspx.cs file might look like this:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace PS.AspDotNet20
{
  public partial class MyPage : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
    }
 
    protected void enterButton_Click(object sender, EventArgs e)
    {
      messageLabel.Text = "Hello " + nameTextBox.Text + "!";
    }
  }
}

Notice that the code-behind class is now a partial class that inherits from Page, so it looks much like code-behind classes in ASP.NET 1.1, with one notable exception: It is no longer necessary to declare member variables corresponding to your server-side controls. When ASP.NET 2.0 processes this page, it will generate two additional classes. When ASP.NET 2.0 handles a request for MyPage.aspx, for example, the first generated class will be a sibling partial class to the code-behind class in MyPage.aspx.cs that contains the control member variable declarations. This sibling partial class looks something like this:

namespace PS.AspDotNet20
{
  public partial class MyPage : IRequiresSessionState
  {
    protected TextBox nameTextBox;
    protected Button enterButton;
    protected Label messageLabel;
    protected HtmlForm form1;
  }
}

The other class that ASP.NET 2.0 will generate is the result of parsing the .aspx file, and it looks similar to the file that is produced today in ASP.NET 1.1 from .aspx page parsing. Note that it derives from your code-behind class (and its sibling partial class). Interestingly, this class is placed in the same temporary source code file as the partial sibling class. Here's what it looks like for the MyPage.aspx file:

namespace ASP
{
  public class MyPage_aspx : PS.AspDotNet20.MyPage
  {
    public MyPage_aspx()
    {
    }
    // methods for building controls here
  }
}
ADVERTISEMENT

For each .aspx file with code-behind you author in ASP.NET 2.0 (beta 2 and later), ASP.NET is going to generate a partial class to be merged with the partial class you define in your code-behind file. These two partial classes will be merged into a single class definition at compilation and will serve as the base class to the class generated from the .aspx file.

Developers accustomed to writing base classes for their .aspx files will continue to do so, but now the control member variables will be generated automatically in a sibling partial class (obviating the need to add them by hand or through the IDE). Note that if you use the src= attribute instead of the CodeFile= attribute to specify your code-behind file (or if you specify neither and deploy the compiled code-behind class assembly in the /bin directory), it will revert to using 1.1-style code-behind and will not generate the sibling partial class with the control declarations. Also note that if you try to convert a 1.1-style code-behind file to use this technique by specifying a CodeFile= attribute, you must remove your control declarations or you will get a compiler error saying they are redundantly defined.

So you can use the new code-behind model with a shared common base class, as many people do today. You can also deploy precompiled base classes (the aspnet_compiler.exe supports precompiling only the code-behind class), so you can deploy a binary assembly along with .aspx files containing layout that you can change on the deployed server.

About the Author
Fritz Onion is a founding partner of Pluralsight, a think-tank organization delivering in-depth technical content and training, where he focuses on Web development with ASP.NET. He is the author of Essential ASP.NET (Addison Wesley) and is currently working on a second edition that will cover ASP.NET 2.0. He frequently publishes articles on .NET in journals such as MSDN Magazine, DOTNETPRO, MSDN Online, and InformIT. He is also a regular speaker at industry conferences and is the track chair for ASP.NET at Win-Dev in Boston.



Back to top










Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTPOnline Home