Ehsan Tavakoli

Writing About .NET Programming and other stuff

Archive for the ‘validation’ Category

Sample use of CompareValidator and CustomValidator for Calendar component

leave a comment »

Here is a very simple sample about how we can use CompareValidator and also CustomValidator for checking calendar selection. In this example, there are two calendars which ask user to select his arrival and departure date. It can be usually used in booking systems. With validators we are trying to ask user to entry proper date at first and then want to force user to book at least for 2 days. We implement the last rule by using CustomValidator…

Here is our default.aspx source:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Booking</title>


<style type="text/css">
div.fixed 
{
    width: 900px;
    margin: 0px auto;
}

div.left 
{
    float: left;
    width: 450px;
}

div.right 
{
    float: right;
    width: 450px;
}
</style>


</head>
<body>
    <form id="form1" runat="server">
        <div class="fixed">
            <div class="left">
                <p>Select your arrival date:</p>
                

                <asp:TextBox ID="arriveTxt" runat="server"></asp:TextBox>
                <asp:Calendar ID="arrive" runat="server" 
                    onselectionchanged="arrive_SelectionChanged"></asp:Calendar>        
            </div>
            <div class="right">
                <p>Select your departure date:</p>
                

                <asp:TextBox ID="departureTxt" runat="server"></asp:TextBox>
                <asp:Calendar ID="departure" runat="server" 
                    onselectionchanged="departure_SelectionChanged"></asp:Calendar>    
            </div>
        
            <asp:CompareValidator ID="validator1" runat="server" ControlToValidate="departureTxt" ControlToCompare="arriveTxt" Type="Date" Operator="GreaterThan" Text="Please Select a Proper Date For Departure/Arrive"></asp:CompareValidator>
        
    
            

            <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
            <asp:CustomValidator ID="validator2" runat="server" ControlToValidate="departureTxt" OnServerValidate="ServerDateValidation">You Must at Least Book for 2 Days</asp:CustomValidator>
            

        
        </div>
    </form>
</body>
</html>

And this is our code behinde:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) {
            departure.SelectedDate = DateTime.Now.Date;
            arrive.SelectedDate = DateTime.Now.Date;
        
            departureTxt.Text = departure.SelectedDate.ToShortDateString();
            arriveTxt.Text = arrive.SelectedDate.ToShortDateString();

       }
    
    
    
    }


    protected void departure_SelectionChanged(object sender, EventArgs e)
    {
        departureTxt.Text = departure.SelectedDate.ToShortDateString();
    }

    protected void arrive_SelectionChanged(object sender, EventArgs e)
    {
        arriveTxt.Text = arrive.SelectedDate.ToShortDateString();

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
    
    }
    protected void ServerDateValidation(object source, ServerValidateEventArgs args)
    {
        System.TimeSpan datediff = departure.SelectedDate - arrive.SelectedDate;

        if (datediff.Days > 2)
            args.IsValid = true;
        else
            args.IsValid = false;

    }
}
Advertisements

Written by Ehsan Tavakoli

February 16, 2010 at 12:13 am