How to Compare Two Calender Extender In Asp.Net

How to Compare Two Calender Extender In Asp.Net

In this tutorial we will learn How to Compare Two Calender Extender In Asp.Net. Here we compare two dates in asp.net using two textbox controls and two ajax calender extender controls. You can use asp.net calender controls instead of ajax calender extender controls but date formats must be same for both textbox’s date values.

Design:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to Compare Two Calender Extender In Asp.Net</title>
<style type="text/css">
body
 {
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 20px;
 }
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
<div>
<h2>How to Compare Two Calender Extender In Asp.Net</h2>
<br /><br /><br /><br />

Start Date: <asp:TextBox ID="startdate" runat="server"></asp:TextBox>
End Date: <asp:TextBox ID="enddate" OnTextChanged="enddate_TextChanged" AutoPostBack="true" runat="server"></asp:TextBox>

<asp:CalendarExtender ID="CalendarExtender1" TargetControlID="startdate" runat="server"></asp:CalendarExtender>
<asp:CalendarExtender ID="CalendarExtender2" TargetControlID="enddate" runat="server"></asp:CalendarExtender>

<br /><br />
<br /><br />
All rights reserved by <a href="http://www.hightechnology.in">www.Hightechnology.in</a> |
     Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>  
</div>
</form>
</body>
</html>

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void enddate_TextChanged(object sender, EventArgs e)
    {
        DateTime a = DateTime.Parse(startdate.Text);
        DateTime b = DateTime.Parse(enddate.Text);

        if (a > b)
        {
        Response.Write("<script>alert('End Date Should be Less then Start Date');</script>");
        enddate.Text="";
        }
     }
}

demo