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

<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>

Then we go on to validate the Departure Date and Return Date TextBox entries. After we established our RequiredFieldValidator controls, we added a CustomValidator control to our page -- the event handler we assigned to the OnServerValidate event property was ValidateTravelData:

<asp:CustomValidator id="validateFlightDates" runat="server"        
 ControlToValidate="flightDepartureDateTextBox"          
 OnServerValidate="ValidateTravelData" />

This method is invoked when the form is posted back to the server. In this example, we're concerned about the data entries in the Departure Date and Return Date TextBox controls. We already know that the user entered something -- the RequiredFieldValidator controls handled that task. We still don't know if the dates the user entered were valid however, -- this will be the work of our CustomValidator method handler, ValidateTravelData. Let's review the pertinent sections from step 4:

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

The first thing we do, is to set the IsValid property of the args argument (a ServerValidateEventArgs object) to False:

args.IsValid = False

We're being pessimistic here, but until we validate everything, we want to be sure that nothing passes our validation test until the very end of the method. Next, we declare two variables of type Date:

Dim departDate, returnDate As Date

We perform the work of getting the user's date input within a Try ... Catch block, so we can catch exceptions:

 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

The Date object's static Parse method is called to pass the user's Departure Date entry. An exception will be thrown under two conditions: a null date (no date entered), or a malformed, or incorrect date entered; one that doesn't meet the criteria of the Date datatype. The flightReturnDateTextBox is similarly validated:

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

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

Sponsored Links