Visible month changed event
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Calendar</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="lblMonthChanged" runat="server" />
<asp:Calendar ID="Calendar1" runat="server"
SelectionMode="DayWeekMonth"
CellPadding="7"
CellSpacing="5"
DayNameFormat="FirstTwoLetters"
FirstDayOfWeek="Monday"
NextMonthText="Next >"
PrevMonthText="< Prev"
ShowGridLines="True"
DayStyle-BackColor="White"
DayStyle-ForeColor="Black"
DayStyle-Font-Names="Arial"
OnSelectionChanged="Calendar1_SelectionChanged"
OnDayRender="Calendar1_DayRender"
OnVisibleMonthChanged="Calendar1_VisibleMonthChanged">
<DayHeaderStyle
BackColor="Black"
Font-Names="Arial Black"
ForeColor="White" />
<SelectedDayStyle
BackColor="Cornsilk"
Font-Bold="True"
Font-Italic="True"
Font-Names="Arial"
ForeColor="Blue" />
<SelectorStyle
BackColor="Cornsilk"
Font-Names="Arial"
ForeColor="Red" />
<WeekendDayStyle
BackColor="LavenderBlush"
Font-Names="Arial"
ForeColor="Purple" />
<OtherMonthDayStyle
BackColor="LightGray"
Font-Names="Arial"
ForeColor="White" />
<TodayDayStyle
BackColor="Cornsilk"
Font-Bold="True"
Font-Names="Arial"
ForeColor="Green" />
<NextPrevStyle
BackColor="DarkGray"
Font-Names="Arial"
ForeColor="Yellow" />
<TitleStyle
BackColor="Gray"
Font-Names="Arial Black"
ForeColor="White"
HorizontalAlign="Left" />
</asp:Calendar>
<br/>
<asp:Label id="lblCount" runat="server" />
<br/>
<asp:Label id="lblTodaysDate" runat="server" />
<br/>
<asp:Label id="lblSelected" runat="server" />
<br/>
<table>
<tr>
<td>
Select a month:
</td>
<td>
<asp:DropDownList
id= "ddl"
AutoPostBack="true"
onSelectedIndexChanged = "ddl_SelectedIndexChanged"
runat="server">
<asp:ListItem text="January" value="1" />
<asp:ListItem text="February" value="2" />
<asp:ListItem text="March" value="3" />
<asp:ListItem text="May" value="5" />
<asp:ListItem text="June" value="6" />
<asp:ListItem text="July" value="7" />
<asp:ListItem text="August" value="8" />
<asp:ListItem text="September" value="9" />
<asp:ListItem text="October" value="10" />
<asp:ListItem text="November" value="11" />
<asp:ListItem text="December" value="12" />
</asp:DropDownList>
</td>
<td>
<asp:Button
id="btnTgif"
text="TGIF"
onClick="btnTgif_Click"
runat="server" />
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><b>Day Range</b></td>
</tr>
<tr>
<td>Starting Day</td>
<td>Ending Day</td>
</tr>
<tr>
<td>
<asp:TextBox id= "txtStart" runat="server"
Width="25"
MaxLength="2" />
</td>
<td>
<asp:TextBox id= "txtEnd" runat="server"
Width="25"
MaxLength="2" />
</td>
<td>
<asp:Button id="btnRange" runat="server"
text="Apply"
onClick="btnRange_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Calendar1.VisibleDate = Calendar1.TodaysDate;
ddl.SelectedIndex = Calendar1.VisibleDate.Month - 1;
}
lblTodaysDate.Text = "Today's Date is " + Calendar1.TodaysDate.ToShortDateString();
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
lblSelectedUpdate();
lblCountUpdate();
txtClear();
}
private void lblSelectedUpdate()
{
if (Calendar1.SelectedDate != DateTime.MinValue)
lblSelected.Text = "The date selected is " +
Calendar1.SelectedDate.ToShortDateString();
}
private void lblCountUpdate()
{
lblCount.Text = "Count of Days Selected: " +
Calendar1.SelectedDates.Count.ToString();
}
protected void ddl_SelectedIndexChanged(Object sender, EventArgs e)
{
Calendar1.SelectedDates.Clear();
lblSelectedUpdate();
lblCountUpdate();
Calendar1.VisibleDate = new DateTime(Calendar1.VisibleDate.Year,
Int32.Parse(ddl.SelectedItem.Value), 1);
txtClear();
}
protected void btnTgif_Click(Object sender, EventArgs e)
{
int currentMonth = Calendar1.VisibleDate.Month;
int currentYear = Calendar1.VisibleDate.Year;
Calendar1.SelectedDates.Clear();
for (int i = 1; i <= System.DateTime.DaysInMonth(currentYear,currentMonth); i++)
{
DateTime date = new DateTime(currentYear, currentMonth, i);
if (date.DayOfWeek == DayOfWeek.Friday)
Calendar1.SelectedDates.Add(date);
}
lblSelectedUpdate();
lblCountUpdate();
txtClear();
}
protected void btnRange_Click(Object sender, EventArgs e)
{
int currentMonth = Calendar1.VisibleDate.Month;
int currentYear = Calendar1.VisibleDate.Year;
DateTime StartDate = new DateTime(currentYear, currentMonth,
Int32.Parse(txtStart.Text));
DateTime EndDate = new DateTime(currentYear, currentMonth,
Int32.Parse(txtEnd.Text));
Calendar1.SelectedDates.Clear();
Calendar1.SelectedDates.SelectRange(StartDate, EndDate);
lblSelectedUpdate();
lblCountUpdate();
}
private void txtClear()
{
txtStart.Text = "";
txtEnd.Text = "";
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (!e.Day.IsOtherMonth && e.Day.IsWeekend)
e.Cell.BackColor = System.Drawing.Color.LightGreen;
if (e.Day.Date.Month == 1 && e.Day.Date.Day == 1)
e.Cell.Controls.Add(new LiteralControl("<br/>Happy New Year!"));
}
protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
if ((e.NewDate.Year > e.PreviousDate.Year) ||
((e.NewDate.Year == e.PreviousDate.Year) &&
(e.NewDate.Month > e.PreviousDate.Month)))
lblMonthChanged.Text = "My future's so bright...";
else
lblMonthChanged.Text = "Back to the future!";
Calendar1.SelectedDates.Clear();
lblSelectedUpdate();
lblCountUpdate();
txtClear();
}
}
Related examples in the same category