Calendar Control to show all the months of an year

G

Guest

Hi,

I need to display a calendar that shows all the months of an year. In
that, I need to show different colors for certain events. I know how to
display a calendar for a certain month but I am not sure if there is a way to
display all the months of an year? Please let me know.

Thanks,
Sridhar.
 
N

Nathan Sokalski

The Calendar control is intended for selecting dates, so the only way to
display all the months of the year would be to use multiple Calendar
controls. You could do this using a control such as the Repeater, or just
manually place multiple Calendar controls on your webform. Because the
Calendar control is intended for selecting dates, not displaying them, I do
not believe there is a way to make specific dates appear in different
colors. I would suggest using some other technique to create something that
looks similar to the Calendar control (for example, if you get creative, you
could make a multi-column DataList and use the ItemCreated and ItemDataBound
events to change the color, or you could just write code to generate a
table, or you might be able to find a control on the web that you can
download that does what you want). I don't know how much experience you have
with what or what your ultimate goal is, so you'll have to decide for
yourself what the best path to take is. Good Luck!
 
M

Mark Rae

I need to display a calendar that shows all the months of an year. In
that, I need to show different colors for certain events. I know how to
display a calendar for a certain month but I am not sure if there is a way
to
display all the months of an year? Please let me know.

This is easy enough using the calendar control (actually, 12 calendar
controls), maybe arranged in a convenient 3 x 4 grid.

1) In the page's ViewState, store the first day of the first calendar

2) Create 12 calendar controls in the HTML portion of your ASPX page - call
them something like 'cal0', 'cal1', 'cal2' etc
<asp:Calendar ID="cal0" OnDayRender="DayRender" runat=server
ShowNextPrevMonth="false" OtherMonthDayStyle-ForeColor="DarkGray"><DayStyle
Font-Size="XX-Small" /><DayHeaderStyle Font-Size="XX-Small"
/></asp:Calendar>

3) Set the date of the first calendar to whatever you want your starting
date to be, maybe the current date, e.g.
cal0.TodaysDate = DateTime.Now;
cal0.VisibleDate = DateTime.Now;

4) Increment the starting date of each subsequent calendar e.g.
cal1.VisibleDate = cal0.VisibleDate.AddMonths(1);
cal2.VisibleDate = cal1.VisibleDate.AddMonths(1);
etc

5) In your code-behind, retrieve all the events which fall between the first
day of the first calendar and 365 days in advance of that date, and store
them in a Hashtable

6) Create a method called DayRender, as follows:

public void DayRender(object source, DayRenderEventArgs e)
{
if (htblDates.ContainsKey(e.Day.Date.ToString("dd MMM yyyy")) &&
!e.Day.IsOtherMonth)
{
e.Cell.Font.Bold = true;
}
}

There are loads of other things you can do - you could change the day's
background colour, add text underneath it etc, but the above should be
enough to get you started.
 
G

Guest

Thanks Mark.

Mark Rae said:
This is easy enough using the calendar control (actually, 12 calendar
controls), maybe arranged in a convenient 3 x 4 grid.

1) In the page's ViewState, store the first day of the first calendar

2) Create 12 calendar controls in the HTML portion of your ASPX page - call
them something like 'cal0', 'cal1', 'cal2' etc
<asp:Calendar ID="cal0" OnDayRender="DayRender" runat=server
ShowNextPrevMonth="false" OtherMonthDayStyle-ForeColor="DarkGray"><DayStyle
Font-Size="XX-Small" /><DayHeaderStyle Font-Size="XX-Small"
/></asp:Calendar>

3) Set the date of the first calendar to whatever you want your starting
date to be, maybe the current date, e.g.
cal0.TodaysDate = DateTime.Now;
cal0.VisibleDate = DateTime.Now;

4) Increment the starting date of each subsequent calendar e.g.
cal1.VisibleDate = cal0.VisibleDate.AddMonths(1);
cal2.VisibleDate = cal1.VisibleDate.AddMonths(1);
etc

5) In your code-behind, retrieve all the events which fall between the first
day of the first calendar and 365 days in advance of that date, and store
them in a Hashtable

6) Create a method called DayRender, as follows:

public void DayRender(object source, DayRenderEventArgs e)
{
if (htblDates.ContainsKey(e.Day.Date.ToString("dd MMM yyyy")) &&
!e.Day.IsOtherMonth)
{
e.Cell.Font.Bold = true;
}
}

There are loads of other things you can do - you could change the day's
background colour, add text underneath it etc, but the above should be
enough to get you started.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top