Article
Beginning ASP.NET Using VB.NET - Chapter 14: ASP.NET Server Controls
If an exception is thrown, we provide the user with some feedback text, via the feedbackLabel that we created in the <!-- BOOK IT BUTTON SECTION & FEEDBACK --> part of our code file. We then promptly return the execution to the program that called this subroutine, so it is ready for the user to have another go. Try entering Today as your departure date, and Tomorrow as your return date, for example, and you'll see this:

Even after it's confirmed that the user entered two valid dates however, there's still some more work to do. The next validation ensures that the departure date entered is earlier than the return date:
' 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
The code for this section is pretty straightforward, using just an if statement, which sends a feedback message if the departure date is greater then the return date. The code then returns to the state it was in before the validation control was called. Just to make things even more robust, we validate that the departure date is not in the past, using the same technique:
' 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
If we were to attempt to book our trip from New York to London without entering anything into the Departure Date and Return Date boxes, the assigned RequiredFieldValidator controls would kick into action and display a message to the user:

As you may have noticed, date validation can be a bit tedious -- but it's worth the effort! The nice thing about this implementation is that we didn't need to restrict the user to a particular date format for their data entry. Yes, we suggested a format like YYYY/mm/dd, but the user could have entered "10/31/2001" or even "October 31, 2001" -- this is because we called the Date.Parse method, which did the work of parsing the user's date entry.
Let's have a quick look at the button function of the web form. The button itself is very simple -- we just give it some text and define its OnClick event as bookTheTripButton_Click:
<p>
<asp:Button id="bookTheTripButton" runat="server" Text="Book This Trip"
OnClick="bookTheTripButton_Click" />
</p>
When the button is clicked, it sends the form information back to the server, which then processes all the server controls we have used. If our page validates correctly (if args.IsValid = True ) our bookTheTripButton OnClick event is triggered:
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
In the end, when all the dates and logic are validated, our trip from New York to London will be booked with a simple click of the mouse:
