Demonstrate the use of Calendar control to perform following operations. a) Display messages in a calendar control b) Display vacation in a calendar control c) Selected day in a calendar control using style d) Difference between two calendar dates

 Default.aspx 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 

Inherits="CalenderControl.Default" %> 

 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head runat="server"> 

</head> 

<body> 

    <form id="form1" runat="server"> 

        <div> 

            <asp:Calendar ID="Calendar1" runat="server" BackColor="White" 

BorderColor="#3366CC" BorderWidth="1px" CellPadding="1" DayNameFormat="Shortest" 

Font-Names="Verdana" Font-Size="8pt" ForeColor="#003399" Height="258px" 

OnSelectionChanged="Calendar1_SelectionChanged" Width="353px"> 

                <DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" /> 

                <NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" /> 

                <OtherMonthDayStyle ForeColor="#999999" /> 

                <SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" /> 

                <SelectorStyle BackColor="#99CCCC" ForeColor="#336666" /> 

                <TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px" 

Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" /> 

                <TodayDayStyle BackColor="#99CCCC" ForeColor="White" /> 

                <WeekendDayStyle BackColor="#CCCCFF" /> 

            </asp:Calendar> 

        </div>         <p> 

            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Result" />             <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Reset" /> 

        </p> 

        <asp:Label ID="Label1" runat="server" Text=""></asp:Label> 

        <p> 

            <asp:Label ID="Label2" runat="server" Text=""></asp:Label> 

        </p> 

        <asp:Label ID="Label3" runat="server" Text=""></asp:Label> 

        <p> 

            <asp:Label ID="Label4" runat="server" Text=""></asp:Label> 

        </p> 

        <asp:Label ID="Label5" runat="server" Text=""></asp:Label> 

    </form> 

</body> 

</html> 

Default.aspx.cs 

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Web; 

using System.Web.UI; 

using System.Web.UI.WebControls; 

 

namespace CalenderControl 

    public partial class Default : System.Web.UI.Page 

    { 

        protected void Page_Load(object sender, EventArgs e) 

        { 

        } 

        protected void Button1_Click(object sender, EventArgs e) 

        { 

            Calendar1.Caption = "Rupam Solutions"; 

            Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday; 

            Calendar1.NextPrevFormat = NextPrevFormat.ShortMonth; 

            Calendar1.TitleFormat = TitleFormat.Month; 

 

            Label2.Text = "Todays Date" + Calendar1.TodaysDate.ToShortDateString(); 

            Label3.Text = "Ganpati Vacation Start: 9-2-2019"; 

            TimeSpan d = new DateTime(2019, 9, 2) - DateTime.Now; 

            Label4.Text = "Days Remaining For Ganpati Vacation:" + d.Days.ToString(); 

            TimeSpan d1 = new DateTime(2019, 12, 31) - DateTime.Now;             

            Label5.Text = "Days Remaining for New Year:" + d1.Days.ToString();            

            if (Calendar1.SelectedDate.ToShortDateString() == "9-2-2019")                 

            Label3.Text = "<b>Ganpati Festival Start</b>"; 

            if (Calendar1.SelectedDate.ToShortDateString() == "9-12-2019") 

                Label3.Text = "<b>Ganpati Festival End</b>"; 

        } 

        protected void Calendar1_DayRender(object sender, 

        System.Web.UI.WebControls.DayRenderEventArgs e) 

        { 

            if (e.Day.Date.Day == 5 && e.Day.Date.Month == 9) 

            { 

                e.Cell.BackColor = System.Drawing.Color.Yellow;                 

                Label lbl = new Label();                 

                lbl.Text = "<br>Teachers Day!";                 

                e.Cell.Controls.Add(lbl);  

            } 

            if (e.Day.Date.Day == 2 && e.Day.Date.Month == 9) 

            { 

                Calendar1.SelectedDate = new DateTime(2019, 9, 2); 

                Calendar1.SelectedDates.SelectRange(Calendar1.SelectedDate, 

                Calendar1.SelectedDate.AddDays(10)); 

                Label lbl1 = new Label();                 

                lbl1.Text = "<br>Ganpati!";                 

                e.Cell.Controls.Add(lbl1); 

            } 

        } 

        protected void Calendar1_SelectionChanged(object sender, EventArgs e) 

        { 

            Label1.Text = "Your Selected Date:" + Calendar1.SelectedDate.Date.ToString(); 

        } 

        protected void Button2_Click(object sender, EventArgs e) 

        { 

            Label1.Text = ""; 

            Label2.Text = ""; 

            Label3.Text = ""; 

            Label4.Text = ""; 

            Label5.Text = ""; 

            Calendar1.SelectedDates.Clear(); 

        } 

    }

 } 

Output:-



Post a Comment

0 Comments