<%@ Page language="c#" %>
<%@ Import Namespace="System.Drawing" %>
<script language="c#" runat="server">
protected void ValidateTravelData(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
args.IsValid = false;
DateTime departDate, returnDate;
feedbackLabel.ForeColor = Color.Red;
try
{
departDate = DateTime.Parse(flightDepartureDateTextBox.Text);
} catch (Exception ex)
{
feedbackLabel.Text = "Invalid data entry: Departure Date is invalid. " +
"Enter a valid date, for example: 01/20/2006";
return;
}
try
{
returnDate = DateTime.Parse(flightReturnDateTextBox.Text);
} catch (Exception ex)
{
feedbackLabel.Text = "Invalid data entry: Return Date is invalid. " +
"Enter a valid date, for example: 01/20/2006";
return;
}
// Verify that the departure date is less than the
// return date - no same day trips in this system!
if (departDate >= returnDate)
{
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;
}
// Verify that the departure date is not in the past or today!
if (departDate < DateTime.Now)
{
feedbackLabel.Text = "Invalid data entry: The Departure Date cannot " +
"be in the past or today!";
return;
}
// Everthing is valid - set the IsValid flag...
args.IsValid = true;
}
private void bookTheTripButton_Click(object sender, EventArgs e)
{
// Has the page been validated for all data entry?
if (!(Page.IsValid))
{
return;
}
// We're all set - book the flight!
DateTime departDate, returnDate;
departDate = DateTime.Parse(flightDepartureDateTextBox.Text);
returnDate = DateTime.Parse(flightReturnDateTextBox.Text);
feedbackLabel.ForeColor = Color.Black;
feedbackLabel.Text = "Success! Your trip from Chicago to London " +
"will depart on the " + departDate.ToLongDateString() +
" and return on the " + returnDate.ToLongDateString();
}</script>
<html>
<head></head>
<body>
<h1>Travel: Chicago to London</h1>
<form id="TravelForm" method="post" runat="server">
<!-- 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>
<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>
</form>
</body>
</html>
|