asp.net - Deselect Calendar selected date on second click

Deselect Calendar selected date on second click
Calendar is an asp.net rich web server control that provide us a way to select an individual dateor date range. this example code demonstrate us an interesting technique to deselect calendar selected date on second click.

first click on any date cell we can select a date from calendar control and we can change the selection of calendar date by selectinganother date or date range from calendar control. calendar server control has no built in feature to deselect selected date on second clickand it has no feature to select random date from calendar control.

to select random date from calendar control and deselect selected date on second click we need to apply few techniques.to do this we created an event handler for calendar SelectionChanged event. we stored the calendar selected dates in a bulletedlist items collection.when users click a date in calendar control then we test it that the buletedlist items contains the clicked date. if we found the selected datein bulletedlist items then it is the second click of same date. if it second click of a date in calendar control then we removed it from bulltedlistitems collection and calendar selected dates collection. finally it deselect the calendar selected date on second click.

the following asp.net c# example code demonstrate us how can we deselect calendar selected date on second click.
DeselectCalendarSelectedDateOnSecondClick.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (Page.IsPostBack && Calendar1.SelectedDates.Count ==1)
        {
            Calendar1.SelectedDates.Clear();
        }
    }

    protected void Calendar1_SelectionChanged(object sender, System.EventArgs e)
    {
        ListItem li = new ListItem();
        li.Text = Calendar1.SelectedDate.ToShortDateString();

        int counter = 0;
        foreach (ListItem litem in BulletedList1.Items)
        {
            if (litem.Text == li.Text)
            {
                counter += 1;
            }
        }

        if (counter > 0)
        {
            BulletedList1.Items.Remove(li);
        }
        else
        {
            BulletedList1.Items.Add(li);
        }

        Calendar1.SelectedDates.Clear();
        SelectedDatesCollection dates = Calendar1.SelectedDates;

        foreach (ListItem litem in BulletedList1.Items)
        {
            DateTime date = Convert.ToDateTime(litem.Text);
            dates.Add(date);
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to deselect Calendar selected date on second click</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:SlateBlue; font-style:italic;">
            How to deselect Calendar selected date on second click
        </h2>
        <hr width="600" align="left" color="SlateGray" />
        <asp:BulletedList
             ID="BulletedList1"
             runat="server"
             Visible="false"
             >
        </asp:BulletedList>
        <asp:Calendar
            ID="Calendar1" 
            runat="server"
            NextPrevFormat="FullMonth"
            ForeColor="WhiteSmoke"
            SelectionMode="Day"
            DayNameFormat="Full"
            Font-Names="Book Antiqua"
            Font-Size="Medium"
            OnSelectionChanged="Calendar1_SelectionChanged"
            >
            <DayHeaderStyle
                 BackColor="OliveDrab"
                 />
            <DayStyle
                 BackColor="DarkOrange"
                 BorderColor="Orange"
                 BorderWidth="1"
                 Font-Bold="true"
                 Font-Italic="true"
                 Font-Size="Large"
                 />
            <NextPrevStyle
                 Font-Italic="true"
                 Font-Names="Arial CE"
                 />
            <OtherMonthDayStyle BackColor="Tomato" />
            <SelectedDayStyle
                 BackColor="Green"
                 BorderColor="MediumSeaGreen"
                 />
            <TitleStyle
                 BackColor="MidnightBlue"
                 Height="36"
                 Font-Size="Large"
                 Font-Names="Courier New Baltic"
                 />
        </asp:Calendar>
    </div>
    </form>
</body>
</html>

asp.net - How to add custom text to a day cell in a Calendar

Add custom text in Calendar Individual Day Cell
Calendar is an ASP.NET web server control. Users can select an individual date or date range in a Calendar control. Calendar OnDayRender() method raises the DayRender event of calendar control that allows us to write a custom handler for the DayRender event. The Calendar DayRender event occurs when each day is created in the control hierarchy for the Calendar control.

We can add static control to a calendar’s specific date cell before Calendar display in a web browser such as Literal, Image, and Hyperlink control. ASP.NET Literal server control reserves a location on the web page to display static text. So if we can add a Literal control on the Calendar individual date cell then we can add any static text to any date cell in the Calendar control.

First, we need to create a Literal control programmatically in the DayRender event handler section. Then we add static text to Literal control which we want to display in the calendar’s specific date cell. After creating the Literal control we add it to the individual date cell. Finally, calendar control displays the static text on the specified date cell. We add the newly created Literal control to Calendar’s specific date cell control collection.

The following ASP.NET C# example code demonstrates to us how can we add custom text on the Calendar individual date cell in an ASP.NET application.
AddCustomTextInCalendarDayCell.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<script runat="server">
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.Date == new DateTime(2008,10,7))
        {
            Literal Literal1 = new Literal();
            Literal1.Text ="<br /><font size=-1 color=Gold>Blog First Post</font>";

            e.Cell.Font.Italic = true;
            e.Cell.Font.Size = FontUnit.XLarge;
            e.Cell.BackColor = System.Drawing.Color.Crimson;
            e.Cell.BorderColor = System.Drawing.Color.Pink;
            e.Cell.ForeColor = System.Drawing.Color.Snow;
            e.Cell.Font.Name = "Courier New";

            e.Cell.Controls.AddAt(1,Literal1);
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to add custom text in Calendar Day Cell</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:SlateBlue; font-style:italic;">
            How to add custom text in Calendar Day Cell
        </h2>
        <hr width="475" align="left" color="SlateGray" />
        <asp:Calendar
            ID="Calendar1" 
            runat="server"
            NextPrevFormat="FullMonth"
            ForeColor="WhiteSmoke"
            SelectionMode="Day"
            DayNameFormat="Full"
            Font-Names="Book Antiqua"
            Font-Size="Medium"
            OnDayRender="Calendar1_DayRender"
            VisibleDate="10/7/2008"
            >
            <DayHeaderStyle
                 BackColor="OliveDrab"
                 />
            <DayStyle
                 BackColor="DarkGreen"
                 BorderColor="ForestGreen"
                 BorderWidth="1"
                 Font-Bold="true"
                 Font-Italic="true"
                 Font-Size="Large"
                 />
            <NextPrevStyle
                 Font-Italic="true"
                 Font-Names="Arial CE"
                 />
            <SelectedDayStyle
                 BackColor="DarkBlue"
                 BorderColor="CornflowerBlue"
                 />
            <TitleStyle
                 BackColor="MidnightBlue"
                 Height="36"
                 Font-Size="Large"
                 Font-Names="Courier New Baltic"
                 />
        </asp:Calendar>
    </div>
    </form>
</body>
</html>

asp.net - How to apply css style on Calendar individual day cell

Apply CSS style in Calendar individual Day Cell
Calendar is an ASP.NET web server control. This web server control allows us to select an individual date or date range at a time. Calendar control is rendered as an HTML table and each day of Calendar control contains a LinkButton control that raises an event when it is clicked.

Calendar control has many built-in properties to set or change its day cell's text color, background color, border style, etc. But Calendar control has no property to change its individual day cell or any specific day cell styles programmatically.

The Calendar DayRender event occurs when each day is created in the control hierarchy of the Calendar control. So it is possible to change the style of any individual day cell in Calendar control before it displays in the web browser. By using this event we can modify the appearance of a specific day cell or multiple cells before it displays on the web page.

The Calendar DayRender event allows us to attach a CSS class for any specific day cell in the Calendar control. In this way, we can apply the core CSS style to specified day cells in the Calendar control. In this example code we applied a mouse hover effect for specific day cells of Calendar control.

The following ASP.NET C# example code demonstrates to us how can we customize the appearance of specified day cells in Calendar control by attaching CSS class to those day cells in an ASP.NET application.
CalendarIndividualDayCellCssStyle.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<script runat="server">
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        DateTime stratDate = new DateTime(2011,12,15);;
        DateTime endDate = new DateTime(2011,12,24);;

        if (e.Day.Date > stratDate && e.Day.Date < endDate)
        {
            e.Cell.Font.Italic = true;
            e.Cell.Font.Size = FontUnit.XLarge;
            e.Cell.Font.Strikeout = true;
            e.Cell.CssClass = "CustomCellCss";
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use CSS style in Calendar individual Day Cell</title>
    <style type="text/css">
        .CustomCellCss a:hover
        {
            background-color:DarkOrange;
            padding: 0px 30px 0px 30px;
            }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:SlateBlue; font-style:italic;">
            How to use CSS style in Calendar individual Day Cell
        </h2>
        <hr width="550" align="left" color="SlateGray" />
        <asp:Calendar
            ID="Calendar1" 
            runat="server"
            NextPrevFormat="FullMonth"
            ForeColor="WhiteSmoke"
            SelectionMode="Day"
            DayNameFormat="Full"
            Font-Names="Book Antiqua"
            Font-Size="Medium"
            OnDayRender="Calendar1_DayRender"
            VisibleDate="12/1/2011"
            >
            <DayHeaderStyle
                 BackColor="DodgerBlue"
                 />
            <DayStyle
                 BackColor="Crimson"
                 BorderColor="IndianRed"
                 BorderWidth="1"
                 Font-Bold="true"
                 Font-Italic="true"
                 />
            <NextPrevStyle
                 Font-Italic="true"
                 Font-Names="Arial CE"
                 />
            <SelectedDayStyle
                 BackColor="Green"
                 BorderColor="SpringGreen"
                 />
            <OtherMonthDayStyle BackColor="DarkRed" />
            <TitleStyle
                 BackColor="MidnightBlue"
                 Height="36"
                 Font-Size="Large"
                 Font-Names="Courier New Baltic"
                 />
        </asp:Calendar>
    </div>
    </form>
</body>
</html>

asp.net - How to use Calendar DayRender event

Calendar DayRender event
The Calendar is an ASP.NET web server control that allows us to select an individual date or date range. The Calendar displays the dates for one month at a time with a total of six weeks appearing at the same time. In an ASP.NET web page Calendar server control is rendered as an HTML table. By default, each day of Calendar control contains a LinkButton control that raises an event when it is clicked.

The Calendar DayRender event occurs when each day is created in the control hierarchy of the Calendar control. DayRender event is raised when each date cell in the Calendar control is created. So it is possible to modify the date cell content and format the individual date cells before it is displayed on the web page.

We can write an event handler for the Calendar DayRender event to customize the content and appearance of any specific date cell or cells. We can add only static control to any date cell such as Literal, Label, HyperLink, and Image. We can change the Calendar default background color, set a background image, and change the text color of any date cell by using this DayRender event. We also can put any text, image, or hyperlink in a date cell.

The following ASP.NET C# example code demonstrates to us how can we use Calendar DayRender event in an ASP.NET application.
CalendarDayRenderEvent.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<script runat="server">
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.DayNumberText == "19" | e.Day.DayNumberText == "21")
        {
            e.Cell.BackColor = System.Drawing.Color.Crimson;
            e.Cell.BorderColor = System.Drawing.Color.Pink;
            e.Cell.Font.Italic = true;
            e.Cell.Font.Size = FontUnit.Large;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use Calendar DayRender event in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:SlateBlue; font-style:italic;">
            How to use Calendar DayRender Event in asp.net
        </h2>
        <hr width="550" align="left" color="SlateGray" />
        <asp:Calendar
            ID="Calendar1" 
            runat="server"
            NextPrevFormat="FullMonth"
            ForeColor="WhiteSmoke"
            SelectionMode="Day"
            DayNameFormat="Full"
            Font-Names="Book Antiqua"
            Font-Size="Medium"
            OnDayRender="Calendar1_DayRender"
            >
            <DayHeaderStyle
                 BackColor="DimGray"
                 />
            <DayStyle
                 BackColor="SlateGray"
                 BorderColor="SkyBlue"
                 BorderWidth="1"
                 Font-Bold="true"
                 Font-Italic="true"
                 />
            <NextPrevStyle
                 Font-Italic="true"
                 Font-Names="Arial CE"
                 />
            <SelectedDayStyle
                 BackColor="Green"
                 BorderColor="SpringGreen"
                 />
            <OtherMonthDayStyle BackColor="Gray" />
            <TitleStyle
                 BackColor="MidnightBlue"
                 Height="36"
                 Font-Size="Large"
                 Font-Names="Courier New Baltic"
                 />
        </asp:Calendar>
    </div>
    </form>
</body>
</html>

asp.net - How to use Calendar SelectionChanged event

How to use Calendar SelectionChanged event in asp.net
Calendar is an asp.net rich web server control. calendar control displays the dates for one month at a time. total six weeksappearing at the same time. users can select an individual date from calendar control. even users can select multiple dates (date range)from a calendar server control.

calendar SelectionChanged event occurs when the users select an individual date or range of dates. by using this event we can determinewhat date or dates the user has selected. we can write an event handler for calendar SelectionChanged event to display user selecteddate or dates in web page after postback.

to get the calendar selected dates we loop through the calendar SelectedDates collection using for loop and display the selected date liston web page.

the following asp.net c# example code demonstrate us how can we use the calendar SelectionChanged event in an asp.net application.
CalendarSelectionChangedEvent.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<script runat="server">
    protected void Calendar1_SelectionChanged(object sender, System.EventArgs e)
    {
        Label1.Text = "";
        for(int i=0; i<Calendar1.SelectedDates.Count; i++)
        {
            Label1.Text += Calendar1.SelectedDates[i].ToLongDateString()+"<br />";
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use Calendar SelectionChanged event in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DodgerBlue; font-style:italic;">
            How to use Calendar SelectionChanged event in asp.net
        </h2>
        <hr width="600" align="left" color="LightBlue" />
        <asp:Label
            ID="Label1" 
            runat="server"
            Font-Size="Large"
            ForeColor="MidnightBlue"
            Font-Italic="true"
            >
        </asp:Label>
        <br />
        <asp:Calendar
            ID="Calendar1" 
            runat="server"
            NextPrevFormat="FullMonth"
            SelectionMode="DayWeekMonth"
            SelectMonthText="Month"
            SelectWeekText="Week"
            ForeColor="WhiteSmoke"
            DayNameFormat="Full"
            Font-Names="Book Antiqua"
            Font-Size="Medium"
            OnSelectionChanged="Calendar1_SelectionChanged"
            >
            <DayHeaderStyle
                 BackColor="DarkOliveGreen"
                 />
            <DayStyle
                 BackColor="DarkKhaki"
                 BorderColor="Khaki"
                 BorderWidth="1"
                 Font-Bold="true"
                 Font-Italic="true"
                 />
            <NextPrevStyle
                 Font-Italic="true"
                 Font-Names="Arial CE"
                 />
            <OtherMonthDayStyle
                 BackColor="SeaGreen"
                 BorderColor="DarkSeaGreen"
                 />
            <SelectedDayStyle
                 BackColor="Green"
                 BorderColor="SpringGreen"
                 />
            <SelectorStyle
                 BackColor="DarkOliveGreen"
                 ForeColor="Snow"
                 Font-Names="Times New Roman Greek"
                 Font-Size="Small"
                 BorderColor="Olive"
                 BorderWidth="1"
                 />
            <TitleStyle
                 BackColor="DarkGreen"
                 Height="35"
                 Font-Size="Large"
                 Font-Names="Courier New Baltic"
                 />
        </asp:Calendar>
    </div>
    </form>
</body>
</html>