The ASP.NET Page Structure Options


ASP.NET 2.0 provides two paths for structuring the code of your ASP.NET pages. The first path utilizes the code-inline model. This model should be familiar to classic ASP 2.0/3.0 developers because all the code is contained within a single .aspx page. The second path uses ASP.NET’s code-behind model, which allows for code separation of the page’s business logic from its presentation logic. In this model, the presentation logic for the page is stored in an .aspx page, whereas the logic piece is stored in a separate class file: .aspx.vb or .aspx.cs.

One of the major complaints about Visual Studio .NET 2002 and 2003 is that it forces you to use the code-behind model when developing your ASP.NET pages because it doesn’t understand the code-inline model. The code-behind model in ASP.NET was introduced as a new way to separate the presentation code and business logic. The following example (called Example 1 because it’s referred to again later) shows a typical .aspx page generated using Visual Studio .NET 2002 or 2003:

  <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"     Inherits="WebApplication.WebForm1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML>  <HEAD>     <title>WebForm1</title>     <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">     <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">     <meta name="vs_defaultClientScript" content="JavaScript">     <meta name="vs_targetSchema"       content="http://schemas.microsoft.com/intellisense/ie5">  </HEAD>  <body>     <form  method=="post " runat=="server ">        <P>What is your name?<br>        <asp:TextBox  runat=="server "></asp:TextBox><BR>        <asp:Button  runat=="server " Text=="Submit "></asp:Button></P>        <P><asp:Label  runat=="server "></asp:Label></P>     </form>  </body> </HTML> 

The code-behind file created within Visual Studio .NET 2002/2003 for the .aspx page is shown in the next example (Example 2, for future reference):

  Public Class WebForm1     Inherits System.Web.UI.Page #Region " Web Form Designer Generated Code "     'This call is required by the Web Form Designer.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()     End Sub     Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox     Protected WithEvents Button1 As System.Web.UI.WebControls.Button     Protected WithEvents Label1 As System.Web.UI.WebControls.Label     'NOTE: The following placeholder declaration is required by the Web Form         Designer.     'Do not delete or move it.     Private designerPlaceholderDeclaration As System.Object     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As       System.EventArgs) Handles MyBase.Init         'CODEGEN: This method call is required by the Web Form Designer         'Do not modify it using the code editor.         InitializeComponent()     End Sub #End Region     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As       System.EventArgs) Handles MyBase.Load         'Put user code to initialize the page here     End Sub     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As       System.EventArgs) Handles Button1.Click         Label1.Text = "Hello " & TextBox1.Text     End Sub End Class 

This code-behind page from ASP.NET 1.0/1.1 shows that a lot of the code that developers never have to deal with is hidden in the #Region section of the page. Because ASP.NET 2.0 is built on top of .NET 2.0, it can now take advantage of the new .NET Framework capability of partial classes. Partial classes enable you to separate your classes into multiple class files, which are then combined into a single class when the application is compiled. Because ASP.NET 2.0 combines all this page code for you behind the scenes when the application is compiled, the code-behind files you work with in ASP.NET 2.0 are simpler in appearance and the model is easier to use. You are presented with only the pieces of the class that you need.

Inline Coding

With the .NET Framework 1.0/1.1, developers went out of their way (and outside Visual Studio .NET) to build their ASP.NET pages inline, avoiding the code-behind model that was so heavily promoted by Microsoft and others. Visual Studio 2005 (as well as Visual Web Developer 2005 Express Edition) enables you to build your pages easily using this coding style. To build an ASP.NET page inline instead of using the code-behind model, you simply select the page type from the Add New Item dialog and make sure that the Place Code in Separate File check box is unchecked. You can access this dialog by right-clicking the project or the solution in the Solution Explorer and selecting Add New Item (see Figure 19-8).

image from book
Figure 19-8

In fact, many page types have options for both inline and code-behind styles. The following table shows your inline options when selecting files from this dialog:

Open table as spreadsheet

File Options Using Inline Coding

File Created

Web Form

.aspx file

Master Page

.master file

Web User Control

.ascx file

Web Service

.asmx file

By using the Web Form option with a few controls, you get a page that encapsulates not only the presentation logic, but the business logic as well:

  <%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Protected Sub Button1_Click(ByVal sender As Object, _        ByVal e As System.EventArgs)         Label1.Text = "Hello " & Textbox1.Text     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Simple Page</title> </head> <body>     <form runat="server">         What is your name?<br />         <asp:Textbox  Runat="server"></asp:Textbox><br />         <asp:Button  Runat="server" Text="Submit"          OnClick="Button1_Click" />         <p><asp:Label  Runat="server"></asp:Label></p>     </form> </body> </html> 

From this example, you can see that all the business logic is encapsulated between <script> tags. The greatest advantage of the inline model is that the business logic and the presentation logic are contained within the same file. Some developers find that having everything in a single viewable instance makes working with the ASP.NET page easier. In addition, Visual Studio 2005 now provides IntelliSense when working with the inline coding model and ASP.NET 2.0. In the past, this capability did not exist. Visual Studio forced you to use the code-behind model; and even if you rigged it so your pages were using the inline model, you lost all IntelliSense capabilities.

New Code-Behind Model

The other option for constructing your ASP.NET 2.0 pages is to build your files using the new code-behind model. We say new because, even though the idea of the code-behind model is the same as it was in previous versions of ASP.NET, the code-behind model used in ASP.NET 2.0 is quite different.

To create a new page in your ASP.NET solution that uses the code-behind model, select the page type you want from the New File dialog. To build a page that uses the code-behind model, you first select the page in the Add New Item dialog and make sure the Place Code in Separate File check box is checked. The following table shows you the options for pages that use the code-behind model:

Open table as spreadsheet

File Options Using Code-Behind

File Created

Web Form

.aspx file

.aspx.vb or .aspx.cs file

Master Page

.master file

.master.vb or .master.cs file

Web User Control

.ascx file

.ascx.vb or .ascx.cs file

Web Service

.asmx file .vb or .cs file

The idea of using the code-behind model is to separate the business logic and presentation logic into separate files. This makes it easier to work with your pages, especially if you are working in a team environment where visual designers work on the UI of the page and coders work on the business logic that sits behind the presentation pieces. In the earlier code labeled Examples 1 and 2, you saw how pages using the code-behind model in ASP.NET 1.0/1.1 were constructed. To see the difference in ASP.NET 2.0, take a look at how its code-behind pages are constructed in the following two examples, the first for the presentation piece and the second for the code-behind piece:

  <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"     Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Simple Page</title> </head> <body>     <form runat="server">         What is your name?<br />         <asp:Textbox  Runat="server"></asp:Textbox><br />         <asp:Button  Runat="server" Text="Submit"          OnClick="Button1_Click" />         <p><asp:Label  Runat="server"></asp:Label></p>     </form> </body> </html> 

Code-behind example:

  Partial Class _Default     Inherits System.Web.UI.Page     Protected Sub Button1_Click(ByVal sender As Object, _        ByVal e As System.EventArgs) Handles Button1.Click         Label1.Text = "Hello " & TextBox1.Text     End Sub End Class 

The .aspx page using this new ASP.NET 2.0 code-behind model has some attributes in the Page directive that are different from those you are familiar with from previous versions of ASP.NET. The first is the CodeFile attribute. This new attribute in the Page directive is meant to point to the code-behind page that is used with this presentation page. In this case, the value assigned is Default.aspx.vb. The second attribute needed is the Inherits attribute. This attribute was available in previous versions of ASP.NET, but was infrequently used. This attribute specifies the name of the class that is bound to the page when the page is compiled. The directives are simple enough in ASP.NET 2.0. Take another look at the preceding code-behind page.

The new code-behind page is rather simple in appearance because of the partial class capabilities that .NET 2.0 provides. You can see that the class created in the code-behind file uses partial classes, employing the new Partial keyword in Visual Basic 2005. This enables you to simply place the methods that you need in your Page class. In this case, you have a button-click event and nothing else.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

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