Article

Beginning ASP.NET Using VB.NET - Chapter 14: ASP.NET Server Controls

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 Next

Page_Unload

Page_Unload serves the opposite purpose of the Page_Load method. The Page_Unload method is used to perform any cleanup just prior to the page being unloaded. It is a virtual method of the Page class, which can be implemented. You would want to implement the Page_Unload method in cases where any of the following actions needed to be performed:

  • Closing files
  • Closing database connections
  • Any other cleanup or discarding of server-side objects and/or resources

The following is an example implementation of the Page_Unload method:

Sub Page_Unload(ByVal Sender As System.Object, ByVal e As      
System.EventArgs)      
' Perform any cleanup here!      
End Sub

One thing to note is that the unloading of a page doesn't happen when you close the browser or move to another page. The Page_Unload event happens when the page has finished being processed by ASP.NET, and before it's sent to the browser.

Try It Out -- "Oh To Travel, Travel, Travel"

Okay, let's get right into this, and do something neat with ASP.NET server controls. In this section, we'll put together a single web form, travel.aspx, that allows us to book a flight from New York to London with only a couple clicks of the mouse! Okay, so it will only be a demo -- we won't actually "book" anything (sigh). We will, however, get some experience building a web form that uses a variety of ASP.NET server controls.

1. Open your code editor and add the following starter lines to layout the Framework for this ASPX page, and save it as travel.aspx:

<%@ Page Language="VB" %>      
<%@ Import Namespace="System.Drawing" %>      
ASP.NET Server Controls 481      
  <script language="VB" runat="server">      
  </script>      
<html>      
<head></head>      
  <body>      
    <h1>Travel: New York to London</h1>      
       <form id="TravelForm"      
method="post" runat="server">      
       <!-- Flight Info -->      
       <!-- BOOK IT BUTTON SECTION &      
FEEDBACK -->      
       </form>      
  </body>      
</html>

We'll be referencing the .NET Framework's Color structure in our code, therefore we needed to add an Import directive referencing the System.Drawing namespace at the top of this page. The remainder of the code consists of the <script></script> tag sections (where we will insert our code for this page), and the two HTML tags to setup our page.

2. In this step, we're going to add some flight date boxes to the page. We're using the ASP.NET panel control to serve as our container for the various controls on our simulated tab. The panel control is a visual tool to display a box, within which other controls can be rendered. Add the following lines:

<!-- Flight Info -->      
<asp:panel id="Panel" runat="server" Width="504px" Height="89px"      
BackColor="Wheat">      
     
Departure Date:      
<asp:TextBox id="flightDepartureDateTextBox" runat="server"      
Width="80px" Height="22px"/>      
     
Return Date:      
<asp:TextBox id="flightReturnDateTextBox" runat="server"      
Width="80px" Height="22px"/></br>      
     
<asp:RequiredFieldValidator id="validateFlightDepartureDate"      
runat="server"      
ErrorMessage="Please enter a valid Departure Date. "      
ControlToValidate="flightDepartureDateTextBox" />      
<asp:RequiredFieldValidator id="validateFlightReturnDate"      
runat="server"      
ErrorMessage="Please enter a valid Return Date."      
     
ControlToValidate="flightReturnDateTextBox" />      
<asp:CustomValidator id="validateFlightDates"      
runat="server"      
ControlToValidate="flightDepartureDateTextBox"      
OnServerValidate="ValidateTravelData" />      
     
</asp:panel>

Note here that we added three validation controls: two RequiredFieldValidator controls, and a CustomValidator control. These controls will serve to force the user to enter a value into the Departure Date and Return Date TextBox controls. The CustomValidator control will raise an OnServerValidate event, and call our ValidateTravelData method, which will perform the work of validating that the date entries make sense logically (for example, the departure date can't be later than the return date). The RequiredFieldValidator control ensures that something is entered into the field, so the user can't skip an entry.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links