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

3. The next control we'll add to this form is the bookTheTripButton, which will serve to post the form data to the server, and the feedbackLabel label control will serve to provide instructions, and offer feedback in the event of an invalid data entry. Add the following lines:

<!-- BOOK IT BUTTON SECTION & FEEDBACK -->        
<p>        
<asp:Button id="bookTheTripButton" runat="server"        
Text="Book This Trip"      
OnClick="bookTheTripButton_Click" />      
</p>        
<p> <asp:Label id="feedbackLabel" runat="server"        
BackColor="Wheat"        
Font-Bold="True"      
Text="Select your options, then click the 'Book This Trip' button!" />        
</p>

4. Recall from step 2 that we added two RequiredFieldValidator controls and the CustomValidator control. In this section, we'll write the event handler for the OnServerValidate event, the ValidateTravelData method, to validate our dates and their logic. Add the following lines between the script tags:

<script language="VB" runat="server">        
Protected Sub ValidateTravelData (source As Object, _      
 args As System.Web.UI.WebControls.ServerValidateEventArgs)      
' Since we have a bit to validate      
' assume that the entry is invalid...      
args.IsValid = False        
     
Dim departDate, returnDate As Date        
feedbackLabel.ForeColor = Color.Red        
     
Try        
 departDate = Date.Parse (flightDepartureDateTextBox.Text)        
Catch ex As Exception        
 feedbackLabel.Text = "Invalid data entry: Departure Date        
 is invalid. " _      
   + "Enter a valid date, for example: 2001/07/04"        
 Return        
End Try        
     
Try      
 returnDate = Date.Parse(flightReturnDateTextBox.Text)      
Catch ex As Exception      
 feedbackLabel.Text = "Invalid data entry: Return Date        
is invalid. " _      
   + "Enter a valid date, for example: 2001/07/04"        
Return      
End Try            
     
' Verify that the departure date is less than the      
' return date - no same day trips in this system!      
If (departDate >= returnDate) Then      
 feedbackLabel.Text = "Invalid data entry: The Departure Date        
must be " _        
 + "earlier than the Return Date and no same-day " _        
 + "returns for this travel package!"      
 Return      
End If            
     
' Verify that the departure date is not in the past or today!      
If (departDate < Date.Now) Then      
 feedbackLabel.Text = "Invalid data      
entry:  The Departure        
Date cannot " _        
 + "be in the past or today!"      
 Return      
End If      
     
 ' Everthing is valid - set the IsValid flag...      
 args.IsValid = True      
     
End Sub      
</script>

5. The final step in this travel.aspx example, is to write an event handler for the bookTheTripButton's OnClick event. Add the following lines to your <script> block, after the lines we just added:

<script language="VB" runat="server">        
...        
Private Sub bookTheTripButton_Click(ByVal sender As System.Object, _      
 ByVal e As System.EventArgs)        
     
' Has the page been validated for all data entry?        
If (Not Page.IsValid) Then        
 Return        
End if        
     
' We're all set - book the flight!        
Dim departDate, returnDate As Date        
departDate = Date.Parse(flightDepartureDateTextBox.Text)        
returnDate = Date.Parse(flightReturnDateTextBox.Text)        
     
feedbackLabel.ForeColor = Color.Black        
feedbackLabel.Text = "Success! Your trip from New York to London " _        
   + "will depart on the " _        
   + departDate.ToLongDateString() _        
   + " and return on the " _        
   + returnDate.ToLongDateString()        
End Sub        
</script>

If you load travel.aspx in your browser, you should see something like the following page:

14_18_1.jpg

In the <!-- Flight Info --> section of travel.aspx, we begin by creating the panel, in which we will contain our text boxes, and set its colour, width and height (in pixels). We then create our Departure Date, and Return Date text boxes, again specifying their size.

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

Sponsored Links