Codebehind


One of the big changes in this release of ASP.NET 2.0 is the way you specify a codebehind class for a page. We'll start with a review of how codebehind classes work in ASP.NET 1.x (which is still supported), and then introduce the new codebehind model introduced in ASP.NET 2.0.

Codebehind Basics

ASP.NET 1.0 introduced a new mechanism for separating programmatic logic from static page layout called codebehind. This technique involves creating an intermediate base class that sits between the Page base class and the machine-generated class from the .aspx file. The intermediate base class derives directly from Page, and the class generated from the .aspx file derives from the intermediate base class instead of directly from Page. With this technique, you can add fields, methods, and event handlers in your codebehind class and have these features inherited by the class created from the .aspx file, eliminating the need to sprinkle code throughout the .aspx file.

Listings 1-6 and 1-7 show a sample .aspx file and its corresponding codebehind file using the 1.0 inheritance model. Note the use of the Src attribute in the Page directive that tells ASP.NET which file to compile to create the base class for this page. You can also leave off the Src attribute altogether and compile the codebehind file yourself, placing the resulting assembly in the /bin directory of your application (this is, in fact, the most common deployment model with Visual Studio .NET 2003).

Listing 1-6. SimplePageWithCodeBehindV1.aspx

<%@ Page Language="C#" AutoEventWireup="true"           src="/books/4/169/1/html/2/SimplePageWithCodeBehind.aspx.cs"           Inherits="EssentialAspDotNet.SimplePageWithCodeBehindV1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Simple page with codebehind V1</title> </head> <body>     <form  runat="server">     <h1>Test ASP.NET 2.0 Page with codebehind V1</h1>     <asp:BulletedList runat="server" >         <asp:ListItem>Sample Item 1</asp:ListItem>         <asp:ListItem>Sample Item 2 ...</asp:ListItem>     </asp:BulletedList>     <h2 runat="server" >Total number of items = xx</h2>     </form> </body> </html> 

Listing 1-7. SimplePageWithCodeBehindV1.aspx.cs

using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace EssentialAspDotNet {   public class SimplePageWithCodeBehindV1 : Page   {     protected BulletedList       _displayList;     protected HtmlGenericControl _messageH2;     string[] _displayItemData =               {"Item #1", "Item #2", "Item #3", "Item #4",                "Item #5", "Item #6", "Item #7", "Item #8",                "Item #9", "Item #10"};     protected override void OnLoad(EventArgs e)     {       _messageH2.InnerText = "Total number of items = " +                              _displayItemData.Length.ToString();       _displayList.DataSource = _displayItemData;       _displayList.DataBind();       base.OnLoad(e);     }   } } 

Codebehind 2.0

In this release of ASP.NET, the codebehind mechanism has changed slightly (although the existing 1.0 syntax is still completely supported). The change is so subtle that you may not even notice that it has changed unless you look really closely. Listings 1-8 and 1-9 show the new syntax using the same page as the previous example.

Listing 1-8. SimplePageWithCodeBehind.aspx using new 2.0 model

<%@ Page Language="C#" AutoEventWireup="true"           CodeFile="SimplePageWithCodeBehind.aspx.cs"           Inherits="EssentialAspDotNet.SimplePageWithCodeBehind" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Simple page with codebehind</title> </head> <body>     <form  runat="server">     <h1>Test ASP.NET 2.0 Page with codebehind</h1>     <asp:BulletedList runat="server" >         <asp:ListItem>Sample Item 1</asp:ListItem>         <asp:ListItem>Sample Item 2 ...</asp:ListItem>     </asp:BulletedList>     <h2 runat="server" >Total number of items = xx</h2>     </form> </body> </html> 

Listing 1-9. SimplePageWithCodeBehind.aspx.cs using new 2.0 model

using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace EssentialAspDotNet {   public partial class SimplePageWithCodeBehind : Page   {     string[] _displayItemData =               {"Item #1", "Item #2", "Item #3", "Item #4",                "Item #5", "Item #6", "Item #7", "Item #8",                "Item #9", "Item #10"};     protected override void OnLoad(EventArgs e)     {       _messageH2.InnerText = "Total number of items = " +                              _displayItemData.Length.ToString();       _displayList.DataSource = _displayItemData;       _displayList.DataBind();       base.OnLoad(e);     }   } } 

There are two significant differences between this model and the standard 1.x model: the introduction of the CodeFile attribute in the @Page directive and the declaration of the codebehind class as a partial class. As you start building the page, you will notice another difference: server-side controls no longer need to be explicitly declared in your codebehind class, but you still have complete access to them programmatically, as shown in Listing 1-9.

The reason this works has to do with the partial keyword applied to your codebehind class. In addition to turning your .aspx file into a class definition with methods for rendering the page, as it has always done, ASP.NET now will also generate a sibling partial class for your codebehind class that contains protected control member variable declarations. Your class is then compiled together with this generated class definition, merged together, and then it becomes the base class for the class generated for the .aspx file. The end result is that you essentially write codebehind classes the way you always have, but you no longer have to declare (or let the designer declare for you) member variable declarations of server-side controls. This was always a somewhat fragile relationship in 1.x, since if you ever accidentally modified one of the control declarations so that it no longer matched the ID of the control declared on the form, things suddenly stopped working. Now the member variables are declared implicitly and will always be correct. Listings 1-10, 1-11, and 1-12 show the relationship between your codebehind class and the ASP.NET-generated classes.

Listing 1-10. Class for .aspx file generated by ASP.NET

namespace ASP {   public class samplepagewithcodebehind_aspx :                      EssentialAspDotNet.SamplePageWithCodeBehind   {     ...   } } 

Listing 1-11. Sibling partial class generated by ASP.NET

namespace EssentialAspDotNet {   public partial class SamplePageWithCodeBehind   {     protected BulletedList       _displayList;     protected HtmlGenericControl _messageH2;     protected HtmlForm           _form1;     ...   } } 

Listing 1-12. Codebehind partial class that you write

namespace EssentialAspDotNet {   public partial class SamplePageWithCodeBehind : Page   {     string[] _displayItemData =               {"Item #1", "Item #2", "Item #3", "Item #4",                "Item #5", "Item #6", "Item #7", "Item #8",                "Item #9", "Item #10"};     protected override void OnLoad(EventArgs e)     {       _messageH2.InnerText = "Total number of items = " +                              _displayItemData.Length.ToString();       _displayList.DataSource = _displayItemData;       _displayList.DataBind();       base.OnLoad(e);     }   } } 

Note that this partial class model is only used if you use the CodeFile keyword in your @Page directive. If you use the Inherits keyword without CodeFile (or with the Src attribute instead), ASP.NET reverts to the 1.1 codebehind style and simply places your class as the sole base class for the .aspx file. Also, if you have no codebehind at all, the class generation acts very much the same as it does in 1.1. Since ASP.NET 2.0 is backward compatible with 1.1, we now have a range of codebehind options at our disposal as Web developers. Visual Studio 2005 uses the new partial class codebehind model for any WebForms, and it will also happily convert Visual Studio .NET 2003 projects to using the new model as well if you use the Conversion wizard. It is best, if possible, to convert all files to the new codebehind model, since some of the new features of ASP.NET 2.0 depend on it.[2]

[2] As an example, strongly typed access to the Profile property bag is added to the sibling partial class for codebehind classes in 2.0, but if you use the 1.1 codebehind model, that strongly typed accessor is added directly to the .aspx-generated class definition, and it will be unavailable to your codebehind class. This is also true for strongly typed master page and previous page access.




Essential ASP. NET 2.0
Essential ASP.NET 2.0
ISBN: 0321237706
EAN: 2147483647
Year: 2006
Pages: 104

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net