Code Separation
We saw earlier that files created with code separation now show in Visual Studio .NET "Whidbey" as two files. In ASP.NET versions 1.0 and 1.1, the code-behind model allowed separation of code from content, and in ASP.NET 2.0 this model has been changed to simplify development. Instead of the content page inheriting from the code-behind page, we now specify which code-behind file is to be compiled with the content file. That may seem like just a change in semantics, but it's actually a more fundamental change, made at the Common Language Runtime (CLR) level.Version 2.0 of the CLR provides support for partial classes, where the same class can span multiple files. This allows the code-separation page to be far simpler than in previous versions because it can be part of the same class as the content page, meaning no more public variables are required to reference the controls on a page.The implementation of this is easy. Consider the page for Authors.aspx (Listing 2.1).
Listing 2.1 Using Code Separationthe ASP.NET Page
<%@ Page compileWith="Authors.aspx.vb"
className="ASP.authors_aspx" %>
<form runat="server">
<asp:Button runat="server" onClick="button_Click" />
<asp:Label runat="server" id="Message" />
</form>
Because of partial classes, the code-separation file (Authors.aspx.vb) is simple, as shown in Listing 2.2.
Listing 2.2 Using Code Separationthe Code-Behind File
Namespace ASP
Expands Class Authors_asp
Sub button_Click(Sender As Object, E As EventArgs)
Message.Text = "You pressed the button"
End Sub
End Class
End Namespace
Partial classes have introduced the new Expands keyword, indicating that this class is not self-contained and is part of the Authors_asp class. For the content page, the compileWith attribute defines the physical file containing the code to compile along with the content page, and the className attribute indicates the name (including the namespace) of the class. When the className attribute of the content file and the namespace and Class of the code file match, code for both files is compiled into a single class.
• Table of ContentsA First Look at ASP.NET v. 2.0By
Publisher: Addison WesleyPub Date: October 23, 2003ISBN: 0-321-22896-0Pages: 528
"This book provides a first look at the new major release of ASP.NET. The authors have worked closely with the ASP.NET team at Microsoft to make sure that this book is authoritative, accurate, and informative. Anyone using ASP.NET will find a wealth of useful info on the next version."
-Scott Guthrie, Product Unit Manager, Web Platforms and Tools Team, Microsoft Corporation
A First Look at ASP.NET v. 2.0 systematically prepares you for Microsoft''''s ASP.NET 2.0 Web development platform, showing how to use it to reduce development time and costs, improve the performance of Web applications and services, simplify deployment and management, and extend your applications to mobile clients.
Drawing on extraordinary insider access to Microsoft''''s ASP.NET development team, three renowned ASP.NET experts cover every key platform enhancement, from Master Page templates for standardizing look and feel to improved security and data- access controls. They go far beyond already published white papers and previews, presenting detailed development techniques, best practices, and sample code that will give you a running start with ASP.NET 2.0.
This book''''s detailed coverage includes:
Streamlining database integration with "code-free" data binding and ASP.NET 2.0''''s new data source controls
Using Master Pages to improve your control over application look and feel
Strengthening security with membership providers, security server controls, cookieless forms authentication, and the new Role Manager
Simplifying personalization with ASP.NET 2.0 ThemesBuilding fast, flexible portals with Web Parts and the new Portal Framework
Delivering more effective mobile device support-with less complexity
Previewing new ASP.NET 2.0 controls and changes in existing controls
Using Microsoft''''s powerful new .NET and built-in administration tools
Whether you''''re already developing with ASP.NET, or preparing to migrate from ASP, A First Look at ASP.NET v. 2.0 gives you complete information for assessing ASP.NET 2.0-and making the most of it.