Using the Middle Tier to Provide Presentation Logic

At some point, it's going to happen a requirement to perform some kind of validation on information that a user is entering on a WebForm. Perhaps you have to validate that the user is entering a valid date (pun intended), which usually is all that's necessary to create a rift between requirements and reality. In reality, most developers know that this simple task could be carried out in numerous ways on the client-side, or presentation layer. The requirement though is to limit the client-side code strictly to HTML output. Now the significance of the term middle tier becomes clear: It is the area that handles what happens between the client and the data the client wants to interact with.

Visual Basic.NET has a convenient built-in function, called IsDate, to handle the quandary of date validation. It returns a Boolean value based on whether the information passed to it can be converted to a valid date format. Where you use this function makes the difference. So, let's take a look at this function from the highest level.

Create a page in your Visual Basic.NET WebForms Application and name it datecheck.aspx. On this page, there will only be two elements: a server-side textbox control and a server-side button control. When the button is clicked on, the information is sent to the server where the IsDate function is executed. From there, the result is written back to the Web page. Listing 12.1 shows the complete datecheck.aspx.vb file, and Listing 12.2 is the code for datecheck.aspx.

Listing 12.1 datecheck.aspx.vb
 Public Class datecheck    Inherits System.Web.UI.Page    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox    Protected WithEvents Button1 As System.Web.UI.WebControls.Button #Region "Web Form Designer Generated Code"   'This call is required by the Web Form Designer.   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()   End Sub   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 with the code editor.      InitializeComponent()   End Sub #End Region   Dim Msg As String   Private Sub Page_Load_ (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load      Button1.Text = "Check Date"      TextBox1.Text = DateTime.Now.ToString   End Sub   Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click      Msg = IsDate(Request.Form.Item("TextBox1")).ToString      Msg += "<BR>"      Msg += Request.Form.Item("TextBox1")      If Page.IsPostBack Then        Response.Write(Msg)        Button1.Text = "Date Checked"      End If   End Sub End Class 

In Listing 12.1 there is no infringement on the client. All activity is happening on the server.

Listing 12.2 datecheck.aspx
 <%@ Page Language="vb" AutoEventWireup="false" Codebehind="datecheck.aspx.vb"  graphics/ccc.gifInherits="Novelty1.datecheck"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>datecheck</title> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form  method="post" runat="server"> <asp:TextBox  style="Z-INDEX: 101; LEFT: 10px; POSITION: absolute; TOP: 36px" runat="server" Width="165px" Height="20px"> </asp:TextBox> <asp:Button  style="Z-INDEX: 102; LEFT: 14px; POSITION: absolute; TOP: 73px" runat="server" Width="104px" Height="25px" Text="Button"> </asp:Button> </form> </body> </HTML> 

In Listing 12.2, the runat=server directive is given to the form elements. That forces the actions placed in the datecheck.aspx.vb file to happen. Listing 12.3 illustrates the actual HTML generated and sent to the client. As with Server Controls, the control code itself is never sent to the client; only the HTML that results from the control code being processed on the server is sent.

Listing 12.3 HTML client code
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>datecheck</title> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form name="Form1" method="post" action="datecheck.aspx" > <input type="hidden" name="_VIEWSTATE" value="dDwxNDg5OTk5MzM7dDw7bDxpPDE+Oz47 bDx0PDtsPGk8Mz47PjtsPHQ8cDxwPGw8VGV4dDs+O2w8Q2hlY2sgRGF0ZTs+Pjs+Ozs+Oz4+Oz4+Oz7 7Edv+v9RpR0ZNzrTLDqp+CeaP+Q==" /> <input name="TextBox1" type="text" value="9/30/2002 10:42:52 AM"   graphics/ccc.gifstyle="height:20px;width:165px;Z-INDEX: 101; LEFT: 10px; POSITION: absolute; TOP: 36px" /> <input type="submit" name="Button1" value="Check Date"  style="height:25px;width:104px;Z-INDEX: 102; LEFT: 14px; POSITION: absolute; TOP: 73px" /> </form> </body> </HTML> 

Listing 12.3 demonstrates that almost any application logic could be stored in the middle tier. We deal with the next level when we start working with the database. Connecting to a database and executing a query are a bit more involved than just validating a date; some, but not much, more work is involved, as we show in the following sections.



Database Access with Visual Basic. NET
Database Access with Visual Basic .NET (3rd Edition)
ISBN: 0672323435
EAN: 2147483647
Year: 2003
Pages: 97

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