Jon Hermiz Blog

The guide to programming and analyzing SQL, .NET, and SAP

Cool Tricks With The ASP.net Calendar

UPDATE: Additional tips: More Tips

Been A Long Time

Hello world...sorry I haven't posted in quite some time, we've been real busy at home this Christmas season.  Anyhow I thought I would share some tricks and tips with using the calendar control before you or your company spends some change to buy a control on the internet.

What Is The ASP.NET Calendar Control

The ASP.net calendar control is simply the calendar that comes prepackaged with visual studio and is available as a standard control.  Here she is on the standard control:

And here is what the calendar control may look like:

The Problem

You have been tasked to create a time off tool request involving some pretty complicated things.  Your boss has asked for numerous features in this tool which you hoped he had never asked. 

Da Boss

 

  • I want it to highlight specific days signaling holidays or time off requests. This helps me visualize what type of day the request is.   Color coding is a plus :-).
  • I'd like some nice mouse over effect on days that are not requested.  This helps the user notice a clickable type of cell.
  • O I dunno...I'd like to not only see a monthly view, but a yearly view so that I can quickly glance over an entire year for an employee.
  • O well...I dunno...Impress me...maybe allow a user to click a day on the calendar and have it go to a page with that calendar day being selected.

Umm you know the simple things..besides you have all the tools that the company has purchased for you.

You

Thoughts to self: ***O NO NOT THE CALENDAR CONTROL...BUT CANT WE JUST BUY SOMETHING PRE MADE***

Boss

O by the way...we don't have any money to buy any third party tools...have a great day.  Don't forget to e-mail when its all done.

You

O certainly boss...*cringe*...this should be no sweat *thank God I wore deodorant*.  Quickly images begin flashing in your head about what your boss said:

"I want it to highlight specific days signaling holidays or time off requests. This helps me visualize what type of day the request is.   Color coding is a plus :-)."

"I'd like some nice mouse over effect on days that are not requested.  This helps the user notice a clickable type of cell."

"O I dunno...I'd like to not only see a monthly view, but a yearly view so that I can quickly glance over an entire year for an employee."

"O well...I dunno...Impress me...maybe allow a user to click a day on the calendar and have it go to a page with that calendar day being selected."

**Ok that one is simple response.redirect and send a query string**

How in the world am I going to do all of these with the ASP.net calendar control?  That control is just so dull, so plain, difficult to use. 

Got To Get To Work

Well the ASP.net calendar control is pretty limited, its got a few events you can hook onto.  In fact when I started writing code using this control I was pretty upset because the control just seemed plain, really stale, didn't live up to my expectations.  But I was forced to use it.  Sure you can probably spend 100-200 bucks on a RAD Control...but do you really need to.  Most everything the Boss wants is right there in the calendar control.  So lets tackle it one step at a time.

Lets do the simple one first.  Before we begin, I am putting a caution that this code may need to be cleaned, this could o r could not be production code, it is merely a sample.  Samples help beginners and even advanced programmers learn how they can accomplish a specific task.  So take what you need and modify to fit your needs.  DO NOT EMAIL ME SAYING HOW BAD / GOOD IT IS TO DO A SPECIFIC THING.  TAKE WHAT YOU NEED AND RUN!!!  Ok done with the rant.  Now lets take the simple example of how to create the rollover effect of the calendar.

Rollover Effect

At first you might think to use the PreRender event to handle this.  The problem with the PreRender event in this case will cause your entire calendar to highlight a specific color when you roll over it.  To avoid this let's use the DayRender event this will ensure that the rollover effect is per day and not per calendar.  To do this first decide what color you want to use to highlight the calendar when you rollover it (onMouseOver).  Then decide what color you want to highlight when you are off of the calendar day (onMouseOut).

In this case I will use a light shade of blue (almost a baby blue) as the onMouseOver color and I'll use white for the onMouseOut color.  You can use hex codes or named colors, both work using this code.  You can also obtain the default backcolor property using @BackColor.

Heres some vb.net to help you:


 

 Protected Sub Calendar13_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar13.DayRender
 	 Dim onmouseoverStyle As String = "this.style.backgroundColor='#D4EDFF'"
	 Dim onmouseoutStyle As String = "this.style.backgroundColor='@BackColor'"
	 Dim rowBackColor As String = String.Empty
 e.Cell.Attributes.Add("onmouseover", onmouseoverStyle)
 e.Cell.Attributes.Add("onmouseout", onmouseoutStyle.Replace("@BackColor", rowBackColor))

 If Not e.Day.IsWeekend Then
            e.Cell.Attributes.Add("onmouseover", onmouseoverStyle)
            e.Cell.Attributes.Add("onmouseout", onmouseoutStyle.Replace("@BackColor", rowBackColor))
 End If

End Sub


 So here all you're doing is setting a mouseOver for days that are not weekends.  The result:

Ok so now that we are done with the highlight effect lets look at the example of color coding specific days. 

Color Coding Specific Days In The ASP.net Calendar Control

In this case I assume the reader knows how to use datatables, datasets, and common sql statements such as SELECT, INSERT, UPDATE, etc.  So assume you have a table that has time off requests.  In this table you have a field where you store the time off request date.  You want the days requested off visually displayed on the calendar control in a different color.  To do this you simply write a stored procedure to pull your data.  You then store this data inside of a datatable or a dataset.  Once you have done this you check the e.Day.Date property with the value stored in your data row value.  If the values match you handle this event and you change the e.Cell.BackColor.

In my case I will use a dataset that is stored in a session variable.  You do not need to do it like this, this is just an example.  For instance, you may want to use a data table, or maybe you just want a dataset but you do not want to store it in a session variable.  In any event you preform this code yet again in the DayRender event of the calendar control.

Here is some code that will help make more sense:

 


Protected Sub Calendar13_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar13.DayRender 
    Dim nextDate As DateTime

    Try
           'this part is the actual time off requests
        If Not CType(Session("dsRequests"), DataSet) Is Nothing Then
            For Each dr As DataRow In CType(Session("dsRequests"), DataSet).Tables(0).Rows
                nextDate = CType(dr("VacationDate"), DateTime)
                If nextDate = e.Day.Date Then
                    e.Cell.BackColor = System.Drawing.ColorTranslator.FromHtml(CType(dr("HTMLColor"), String))
              End If
            Next
        End If

    Catch ex As Exception
        Response.Write("Errors occurred: No RuleID / Hire Date specified for user! " & "Additional errors include: " & ex.ToString())
    End Try
End Sub


What's going on in this code.  Well first and foremost get rid of what you don't need and change field names, code based on your needs.  In my table I store a field called VacationDate this field has the date value of the time off request.  I assign this value to nextDate and compare nextDate with e.Day.Date.  If these values are the same then I set the e.Cell.BackColor to a color based on another field in my database HTMLColor.  You do not have to have this field, this field is used because in my circumstances we have many time off request types and I want to associate a hex color with each time off type.  You may want to only use a specific color like, System.Drawing.Color.Red.  In this case you would do:

e.Cell.BackColor =  System.Drawing.Color.Red

So the steps are:

  1. Pull the data using a stored procedure, order it by date asc
  2. Take this data and store it in a dataset or a datatable
  3. Use the DayRender event to handle the event each day
  4. Check if e.Day.Date is equal to your date that was returned from the database (dr("HolidayDate") from above)
  5. If the values match up set the e.Cell.BackColor property to the color you want.  Better yet store a color hex value in your database for each type of time off request.

But I Used The DayRender Event For That OnMouseOver Effect Now What?

Simple, combine both together like so:

    Protected Sub Calendar13_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar13.DayRender
        Dim nextDate As DateTime
        Dim onmouseoverStyle As String = "this.style.backgroundColor='#D4EDFF'"
        Dim onmouseoutStyle As String = "this.style.backgroundColor='@BackColor'"
        Dim rowBackColor As String = String.Empty
        Dim notTouched As Boolean
    notTouched = True

    Try
        If Not CType(Session("dsHolidays"), DataSet) Is Nothing Then
            For Each dr As DataRow In CType(Session("dsHolidays"), DataSet).Tables(0).Rows
                nextDate = CType(dr("HolidayDate"), DateTime)
                If nextDate = e.Day.Date Then
                    e.Cell.BackColor = System.Drawing.Color.Pink
                    notTouched = False
                End If
            Next
        End If

        If Not CType(Session("dsRequests"), DataSet) Is Nothing Then
            For Each dr As DataRow In CType(Session("dsRequests"), DataSet).Tables(0).Rows
                nextDate = CType(dr("VacationDate"), DateTime)
                If nextDate = e.Day.Date Then
                    e.Cell.BackColor = System.Drawing.ColorTranslator.FromHtml(CType(dr("HTMLColor"), String))
                    notTouched = False
              End If
            Next
        End If

        If notTouched And Not e.Day.IsWeekend Then
            e.Cell.Attributes.Add("onmouseover", onmouseoverStyle)
            e.Cell.Attributes.Add("onmouseout", onmouseoutStyle.Replace("@BackColor", rowBackColor))
        End If

    Catch ex As Exception
        Response.Write("Errors occurred: No RuleID / Hire Date specified for user!  " & "Additional errors include: " & ex.ToString())
    End Try

End Sub


 
The OnMouseOver effect as well as the colored background cells on the 7th and the 25th of December 07.
 
But What If I Am Using More Then One Calendar Control
 
There is always those what if's.  You may be using more then one calendar control in your application, if you are no problem.  In this case create a sub routine to HANDLES all the calendar objects in your application.  For instance, assume you have 12 calendar controls then your subroutine signature should look like this:
 

Private Sub DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender, Calendar2.DayRender, _ 
                          Calendar3.DayRender, Calendar4.DayRender, Calendar5.DayRender, Calendar6.DayRender, Calendar7.DayRender, Calendar8.DayRender, Calendar9.DayRender, _
                          Calendar10.DayRender, Calendar11.DayRender, Calendar12.DayRender

This shows one sub routine called DayRender which can handle the DayRender event of any of the 12 calendars.
The rest of the code is very similiar to what we posted previously:

Private Sub DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender, Calendar2.DayRender, _ 
                          Calendar3.DayRender, Calendar4.DayRender, Calendar5.DayRender, Calendar6.DayRender, Calendar7.DayRender, Calendar8.DayRender, Calendar9.DayRender, _
                          Calendar10.DayRender, Calendar11.DayRender, Calendar12.DayRender
    Dim nextDate As DateTime
    Dim cal As Calendar = CType(sender, Calendar)
    Dim onmouseoverStyle As String = "this.style.backgroundColor='#D4EDFF'"
    Dim onmouseoutStyle As String = "this.style.backgroundColor='@BackColor'"
    Dim rowBackColor As String = String.Empty
    Dim notTouched As Boolean

    notTouched = True

    If e.Day.Date.Month <> cal.TodaysDate.Month Then Exit Sub

    Try
        If Not CType(Session("dsRequests"), DataSet) Is Nothing Then
            For Each dr As DataRow In CType(Session("dsRequests"), DataSet).Tables(0).Rows
                nextDate = CType(dr("VacationDate"), DateTime)
                If nextDate = e.Day.Date Then
                    e.Cell.BackColor = System.Drawing.ColorTranslator.FromHtml(CType(dr("HTMLColor"), String))
                    notTouched = False
                End If
            Next
        End If

        If notTouched And Not e.Day.IsWeekend Then
            e.Cell.Attributes.Add("onmouseover", onmouseoverStyle)
            e.Cell.Attributes.Add("onmouseout", onmouseoutStyle.Replace("@BackColor", rowBackColor))
        End If

    Catch ex As Exception
        Response.Write("Errors occurred: No RuleID / Hire Date specified for user! " & "Additional errors include: " & ex.ToString())
    End Try
End Sub


In this case we handle all 12 calendar controls.  The result is as follows:

Passing The Date To Another Page

Suppose you want to pass a calendar day to a Response.Redirect to another page when you click on a calendar day.  Here you can simply pass a parameter to a page like so:

Response.Redirect("entry.aspx?VacationDate=" & CType(Calendar13.SelectedDate, Date))

And to pull that date in the entry.aspx page simply use the following:


Dim d as Date

d = CType(Request("VacationDate"), Date) 'grab date


Hope this helps someone out there who needs to do something similiar.  

Legacy Comments


Justin daWebber
2007-12-10
re: Cool Tricks With The ASP.net Calendar
Dude I have been looking for examples on how to change the background color of a single date. Nice example...saved me a bunch of work. Good looking out!


Chris Pietschmann
2007-12-11
re: Cool Tricks With The ASP.net Calendar
Very nice!

One tip on making this better, make your own class that inherits the Calendar and then add this functionality to it. Then you'll have a nice calendar control with this functionality that you can reuse anywhere.

Jon
2007-12-12
re: Cool Tricks With The ASP.net Calendar
Chris these are just simple examples. If you would like to create a class to reuse this functionality create one and to help others you can post it if you'd like!

Ranch
2007-12-19
re: Cool Tricks With The ASP.net Calendar
Add this near the top of the DayRender function to remove other month's date

//To remove the other month's days
if (e.Day.IsOtherMonth)
{
e.Cell.Text = " ";
}

Jon
2007-12-19
re: Cool Tricks With The ASP.net Calendar
Good point ranch or this: If e.Day.Date.Month <> cal.TodaysDate.Month Then Exit Sub

C Renee
2008-01-24
re: Cool Tricks With The ASP.net Calendar
Sorry if this gets posted twice. How did you get the first column to render as Monday, because when I use the control the first column starts at Sunday.

Jon
2008-01-24
re: Cool Tricks With The ASP.net Calendar
If you right click the calendar and goto properties the properties of that calendar will show up. One of the properties is called "FirstDayOfWeek". Change the default from "Sunday" to "Monday". Try this:

<asp:Calendar ID="Calendar13" runat="server" BorderColor="#24618E" Font-Size="Large" FirstDayOfWeek="Monday" Width="300px">


Christian
2008-05-16
re: Cool Tricks With The ASP.net Calendar
How do I do the same thing, but for all a week.

I do not what to select the day. When the mouse over a day I what the week for that day to be highlight.


Thanks


Prince
2008-06-23
re: Cool Tricks With The ASP.net Calendar
Hey guys,
Can we guys add button and label inside the day?? cna you provide us the snippets or URL

Jon
2008-06-24
re: Cool Tricks With The ASP.net Calendar
Prince I posted that here: http://weblogs.sqlteam.com/jhermiz/archive/2007/12/11/Additional-Tip-For-That-Calendar-Control-In-ASP.net.aspx

Amy
2008-07-17
re: Cool Tricks With The ASP.net Calendar
Can we have a day view or a week view in this web calendar.

Jon
2008-07-17
re: Cool Tricks With The ASP.net Calendar
Kind of...you can write code to do this. For instance let us say you want to see a Day view of the web calendar. You can program the calendar so that when you click on a specific day it takes you to the day view.

James
2008-08-07
re: Cool Tricks With The ASP.net Calendar
Here's a trick for the mouseover effect. This variation resets the background color to what it was prior to the mouseover color, so it doesn't always set it to blue (or some other color). This is useful if you have multiple colors of cells and need them to revert to the original colors.


Dim currentBackgroundVarName As String = "bgColor_" & e.Day.Date.Year & "_" & e.Day.Date.Month & "_" & e.Day.Date.Day
Dim onmouseoverStyle As String = currentBackgroundVarName & " = this.style.backgroundColor; this.style.backgroundColor='#0000FF'"
Dim onmouseoutStyle As String = "this.style.backgroundColor=" & currentBackgroundVarName & ";"

e.Cell.Attributes.Add("onmouseover", onmouseoverStyle)
e.Cell.Attributes.Add("onmouseout", onmouseoutStyle)

Jon
2008-08-07
re: Cool Tricks With The ASP.net Calendar
James,

My code already does just that. Notice when you rollover the pink or white days..they do not get highlighted to blue. It is a lot less string manipulation which is what you are doing.

James
2008-08-07
re: Cool Tricks With The ASP.net Calendar
The difference is that you are not attaching the mouseover/mouseout code to all cells in the calendar. My variation is for a slightly different purpose in allowing all of the days to have a mouseover effect (regardless of what background color they were set to) and have it go back to the original color.

Jon
2008-08-08
re: Cool Tricks With The ASP.net Calendar
Actually it is for ALL cells, the day render event fires for each day.

ramya
2008-08-30
Cool Tricks With The ASP.net Calendar
hi .., i am involve in project in that .., i wamt change the color of the calender what r the date present in database that date and all i want to change the color of the dates.., pls help me thank u

Serpil Boyraz
2008-09-25
re: Cool Tricks With The ASP.net Calendar
Excellent!!! That's what I was looking for.

Eddie BT
2008-11-24
re: Cool Tricks With The ASP.net Calendar
This is a bit difficult for a newcomer like me to follow without the images. Any chance of getting them back?

Jon
2008-11-24
re: Cool Tricks With The ASP.net Calendar
Im not sure what you mean Eddie BT. I see the images just fine...Maybe you are behind some sort of firewall or your browser does not allow for images?

EddieBT
2008-11-24
re: Cool Tricks With The ASP.net Calendar
Sorry. Scrap my last comment - f'ing company web filters.

Jon
2008-11-24
re: Cool Tricks With The ASP.net Calendar
No problem I was just wondering. Let me know if you have any questions or issues.

Mark
2008-12-02
re: Cool Tricks With The ASP.net Calendar
I've been able to add some dynamic linkbuttons to the day, but I can't seem to wire them to a commandeventhandler in the usual way:

thisLink.Command += new CommandEventHandler(this.LinkButton_Command);

Am I missing something?

Jon
2008-12-03
re: Cool Tricks With The ASP.net Calendar
Hi Mark,

If you are dynamically creating linkbuttons you'll want to make certain that the dynamic link buttons be recreated every postback in order for their event handlers to run. There is nothing wrong with your code but I just think that you are forgetting that when a post back happens the events won't possible be in tact still, so just make sure that you check for this.

When a button is clicked an event message is sent in postback to the originating page, so if that dynamic content (recreation of the event handlers) is not there a second time around then you program will not be able to fire the object's event.


Mark
2008-12-04
re: Cool Tricks With The ASP.net Calendar
Thanks Jon for your quick response. Unfortunately, in the interest of time I had to go in a different direction.

Kartin
2008-12-17
re: Cool Tricks With The ASP.net Calendar
Hi Jon
Very nice tutorial :)
Was just wondering on how you got the other 11 calendars to render the correct months?
The only way I was able to do this - and it´s not working correctly - is to manipulate the TodaysDate parameter, setting it to be one month ahead of time. The downside to this is the current date get´s selected on all of the calendars!

How have you worked around this?

Jon
2008-12-18
re: Cool Tricks With The ASP.net Calendar
Hi Kartin,

Couple of things I want to ask you first. Did you make sure your dayrender event handles all 12 calendars?

For instance here is what your signature should say:

Private Sub DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender, Calendar2.DayRender, _
Calendar3.DayRender, Calendar4.DayRender, Calendar5.DayRender, Calendar6.DayRender, Calendar7.DayRender, Calendar8.DayRender, Calendar9.DayRender, _
Calendar10.DayRender, Calendar11.DayRender, Calendar12.DayRender

Jon
2008-12-18
re: Cool Tricks With The ASP.net Calendar
Next thing kartin (sorry I had to do this because the blog was not taking the full message):

Do not use the handler automatically created when you double click a calendar, you will have to define this sub routine and handle all calendars with it.


After that make sure you do the same thing for the selectionchanged event:

Public Sub Calendar_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged, Calendar9.SelectionChanged, Calendar5.SelectionChanged, Calendar2.SelectionChanged, Calendar6.SelectionChanged, Calendar4.SelectionChanged, Calendar3.SelectionChanged, Calendar10.SelectionChanged, Calendar7.SelectionChanged, Calendar8.SelectionChanged, Calendar11.SelectionChanged, Calendar12.SelectionChanged

Jon
2008-12-18
re: Cool Tricks With The ASP.net Calendar
The reason you do this is you want to handle all calendars for a single event, this is different then just handling each calendar in their own event. Finally to create and initialize these calendars here is some code that will help you:

Private Sub LoadYearlyCalendars()
Calendar1.TodaysDate = CDate(&amp;amp;amp;amp;quot;1/1/&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear)
Calendar2.TodaysDate = CDate(&amp;amp;amp;amp;quot;2/1/&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear)
Calendar3.TodaysDate = CDate(&amp;amp;amp;amp;quot;3/1/&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear)
Calendar4.TodaysDate = CDate(&amp;amp;amp;amp;quot;4/1/&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear)
Calendar5.TodaysDate = CDate(&amp;amp;amp;amp;quot;5/1/&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear)
Calendar6.TodaysDate = CDate(&amp;amp;amp;amp;quot;6/1/&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear)
Calendar7.TodaysDate = CDate(&amp;amp;amp;amp;quot;7/1/&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear)
Calendar8.TodaysDate = CDate(&amp;amp;amp;amp;quot;8/1/&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear)
Calendar9.TodaysDate = CDate(&amp;amp;amp;amp;quot;9/1/&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear)
Calendar10.TodaysDate = CDate(&amp;amp;amp;amp;quot;10/1/&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear)
Calendar11.TodaysDate = CDate(&amp;amp;amp;amp;quot;11/1/&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear)
Calendar12.TodaysDate = CDate(&amp;amp;amp;amp;quot;12/1/&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear)

Calendar1.VisibleDate = Calendar1.TodaysDate
Calendar2.VisibleDate = Calendar2.TodaysDate
Calendar3.VisibleDate = Calendar3.TodaysDate
Calendar4.VisibleDate = Calendar4.TodaysDate
Calendar5.VisibleDate = Calendar5.TodaysDate
Calendar6.VisibleDate = Calendar6.TodaysDate
Calendar7.VisibleDate = Calendar7.TodaysDate
Calendar8.VisibleDate = Calendar8.TodaysDate
Calendar9.VisibleDate = Calendar9.TodaysDate
Calendar10.VisibleDate = Calendar10.TodaysDate
Calendar11.VisibleDate = Calendar11.TodaysDate
Calendar12.VisibleDate = Calendar12.TodaysDate

Calendar1.SelectedDates.Clear()
Calendar2.SelectedDates.Clear()
Calendar3.SelectedDates.Clear()
Calendar4.SelectedDates.Clear()
Calendar5.SelectedDates.Clear()
Calendar6.SelectedDates.Clear()
Calendar7.SelectedDates.Clear()
Calendar8.SelectedDates.Clear()
Calendar9.SelectedDates.Clear()
Calendar10.SelectedDates.Clear()
Calendar11.SelectedDates.Clear()
Calendar12.SelectedDates.Clear()

Me.hlNext.NavigateUrl = &amp;amp;amp;amp;quot;Default.aspx?yrtype=next&amp;amp;amp;amp;amp;syear=&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear
Me.hlPrev.NavigateUrl = &amp;amp;amp;amp;quot;Default.aspx?yrtype=previous&amp;amp;amp;amp;amp;syear=&amp;amp;amp;amp;quot; &amp;amp;amp;amp;amp; syear
End Sub

Jon
2008-12-18
re: Cool Tricks With The ASP.net Calendar
Replace all the &amp;quot with double quotes from above...

Call this subroutine from your onload event function of the page. Its a bit messy but you can clean it up to fit your needs, as this is just a sample. By the way syear is defined inside of onload if you want as well:

If Request.QueryString(&amp;amp;amp;amp;quot;syear&amp;amp;amp;amp;quot;) Is Nothing Then
syear = Today.Year
Me.hlNext.ToolTip = (syear + 1).ToString
Me.hlPrev.ToolTip = (syear - 1).ToString
Else
syear = CType(Request.QueryString(&amp;amp;amp;amp;quot;syear&amp;amp;amp;amp;quot;), Integer)
If Request.QueryString(&amp;amp;amp;amp;quot;yrtype&amp;amp;amp;amp;quot;) = &amp;amp;amp;amp;quot;next&amp;amp;amp;amp;quot; Then
syear = syear + 1

Me.hlNext.ToolTip = (syear + 1).ToString
Me.hlPrev.ToolTip = (syear - 1).ToString

ElseIf Request.QueryString(&amp;amp;amp;amp;quot;yrtype&amp;amp;amp;amp;quot;) = &amp;amp;amp;amp;quot;previous&amp;amp;amp;amp;quot; Then
syear = syear - 1
Me.hlNext.ToolTip = (syear + 1).ToString
Me.hlPrev.ToolTip = (syear - 1).ToString
Else
'do nothing
End If
End If

Hope this helps!

litty
2009-02-12
re: Cool Tricks With The ASP.net Calendar
excellent

ts248800
2009-03-18
re: Cool Tricks With The ASP.net Calendar
How can load a sharepoint calendar (date and events) from a sql table?

omarJ
2009-05-22
re: Cool Tricks With The ASP.net Calendar
Excelent it reallye helped me a lot

Lorna
2009-10-07
re: Cool Tricks With The ASP.net Calendar
Hi This is great thanks. My calendar displays a red background for all dates in month which have events and works the first time the page loads, however when I click another date the DayRender function doesn't seem to be called so the background colours are not longer there. Please can you tell me how I can ensure it gets called on postback.

Thanks
Lorna

ds
2009-11-26
re: Cool Tricks With The ASP.net Calendar
how can we show calender icon instead of the whole calender control default size? and when user clicks ,the default size will appear?anyone who knows, plz share...

Ankur Agrawal
2010-01-14
re: Cool Tricks With The ASP.net Calendar
Hi, i want to perform a task when a user moves mouse pointer over a specific highlighted date in the calendar. The user gets the details we want to share for that particular data on mouse hover on that date, and also there is a link given in that information which user can select and then the user will be re-directed to another page. it would be highly appreciated if the link or help is without the usage of Javascript.

William
2010-02-08
re: Cool Tricks With The ASP.net Calendar
Hello,

Your method is working well.
I have one question though: when applying the mouseover-effect, the selected date's color changes as well after hovering it. How do I avoid this?

So I have in code behind:

[CODE]
Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
Dim onmouseoverStyle As String = "this.style.backgroundColor='#D53232'"
Dim onmouseoutStyle As String = "this.style.backgroundColor='@BackColor'"
Dim WeekendDayStyle As String = "this.style.backgroundColor='#F5FFC5'"
Dim rowBackColor As String = String.Empty

e.Cell.Attributes.Add("onmouseover", onmouseoverStyle)
e.Cell.Attributes.Add("onmouseout", WeekendDayStyle)

If Not e.Day.IsWeekend Then
e.Cell.Attributes.Add("onmouseover", onmouseoverStyle)
e.Cell.Attributes.Add("onmouseout", onmouseoutStyle.Replace("@BackColor", rowBackColor))
End If
End Sub
[/CODE]

and

[CODE]
<asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="#999999"
CellPadding="1" DayNameFormat="Shortest" Font-Names="Trebuchet MS" Font-Size="7pt"
ForeColor="Black" Height="180px" Width="180px"
SelectedDate='<%# Convert.ToDateTime(session("seldate")) %>'
VisibleDate='<%# Convert.ToDateTime(session("seldate")) %>'
>
<SelectedDayStyle BackColor="#D53232" Font-Bold="True" ForeColor="White" BorderColor="Black" />
<TodayDayStyle BackColor="#D53232" ForeColor="White" />
<SelectorStyle BackColor="#CCCCCC" />
<WeekendDayStyle BackColor="#F5FFC5" />
<OtherMonthDayStyle ForeColor="#808080" />
<NextPrevStyle VerticalAlign="Bottom" />
<DayHeaderStyle BackColor="#D9EE77" Font-Bold="True" Font-Size="7pt" />
<TitleStyle BackColor="#B9D532" BorderColor="Black" Font-Bold="True" />

</asp:Calendar>
[/CODE]

So selecteddaystyle backcolor is "#D53232". When hovering this canges to set backgroundcolor. How to avoid this?

Thank you in advance!
William.



miglior sito di Poker
2010-03-27
re: Cool Tricks With The ASP.net Calendar
How can i achieve the above funtionality with calendar control or is there any other control in asp.net which serves my purpose. Please provide me the solution in C#.I want to know it as soon a possible..

Damien
2010-08-25
re: Cool Tricks With The ASP.net Calendar
Hi,

Excellent help up above,
Would you be able to give an example of the DataTable/ DataSet that you used for the excellent above example and where you put it in the application???

It would be extremely helpful if you could for a rookie like me.

Thanks in advance,

Damien.

columbia jackets
2010-10-21
re: Cool Tricks With The ASP.net Calendar
O certainly boss...*cringe*...this should be no sweat *thank God I wore deodorant*. Quickly images begin flashing in your head about what your boss said:

"I

snow boots for women | columbia sportswear outlet | cheap mac makeup | the north face jackets

womens snow boots | columbia sportswear | cheap makeup | cheap north face jackets

Dannoman
2011-01-26
re: Cool Tricks With The ASP.net Calendar
I would say if you want this in C#, simply use the vb.net to C#.net converter. By just grabbing a pre-build solution, what will you be learning?

Parimal
2011-06-29
re: Cool Tricks With The ASP.net Calendar
it's excelent...

good bro... keep it up

Victor
2011-07-16
re: Cool Tricks With The ASP.net Calendar
This is excellent relief...
However I would appreciate if you can help me on this exactly but on c#.net language

Thanks

Samir moussa
2011-08-27
re: Cool Tricks With The ASP.net Calendar
tk u for this great article realy

im also have the same task exactly so i have some questions.

1- i need to make each calneder select specific month start from jan to decamber
2- if u can help me by source good i wil be gratful for u for u really.



Nick
2011-09-01
re: Cool Tricks With The ASP.net Calendar
Hi everyone,

Just checked out this code, very helpful !

Concerning the "My background color changes and doesn't come back" comment, here is what I did :

Protected Sub Main_Calendar_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Main_Calendar.DayRender
Dim onmouseoverStyle As String = "this.style.backgroundColor='#D4EDFF'"
Dim DayColor As String = "this.style.backgroundColor='#99CCCC'"
Dim BackColor As String = "this.style.backgroundColor='White'"



If e.Cell.BackColor = Drawing.Color.FromArgb(153, 204, 204) Then
If Not e.Day.IsWeekend Then
e.Cell.Attributes.Add("onmouseover", onmouseoverStyle)
e.Cell.Attributes.Add("onmouseout", DayColor)

End If

Else
If Not e.Day.IsWeekend Then
e.Cell.Attributes.Add("onmouseover", onmouseoverStyle)
e.Cell.Attributes.Add("onmouseout", BackColor)
End If
End If

End Sub

I created as many variables as different colors on the calendar.
Then, with an "if" statement, I check the color of the day. If it's blue, it will become blue again on "mouseout", or white otherwise.


Hope this helped.

Nick

Jean
2011-10-20
re: Cool Tricks With The ASP.net Calendar
Hi thank you for providing such a nice and wonderful tutorial but i have a question. u reply to a person saying Replace all the &amp;quot with double quotes from above can i know whta does it mean? is it :amp;" or way?

vikram
2011-11-25
re: Cool Tricks With The ASP.net Calendar
Mind Blowing Work Dude thanks :)

Claus
2011-12-29
It won't AutoPostBack
This is indeed an excellence piece of code.

I like to use it with a calendar that performs AutoPostBack in order to calculate a rate based on the selected date.

That works until I reach this line:
Try
If Not CType(Session("dsRequests"), DataSet) Is Nothing Then
For Each dr As DataRow In CType(Session("dsRequests"), DataSet).Tables(0).Rows <<<--

Right here AutoPostBack stops working.

Can anyone reveal why and what to make it work?

Kind regards,
Claus

Ram
2012-01-11
re: Cool Tricks With The ASP.net Calendar
I have used calendar control and I'm showing events on particular dates.
I want those events to be clickable and should pass event id so that on next page details of events can be displayed.

Any help ? Need to do in C#

organizadores de bodas
2012-02-06
re: Cool Tricks With The ASP.net Calendar
Great article!! Thanks so much!

vipul
2012-02-07
re: Cool Tricks With The ASP.net Calendar
i want to display a calendar on my drop down list selection..i mean,if i select march from my drop down list,it shoul display calendar for that month only..can anyone help????

Trapper
2012-02-19
re: Cool Tricks With The ASP.net Calendar
Does anyone know a way to get the background color of any given day split diagonally? (e.g. half white / half gray )

This would represent either the morning or afternoon of the day displayed rather than the entire day.

Cheers.

Marlo
2012-02-23
re: Cool Tricks With The ASP.net Calendar
Hi Sir, where to add this code?

Response.Redirect("entry.aspx?VacationDate=" & CType(Calendar13.SelectedDate, Date))

planB
2012-04-12
re: Cool Tricks With The ASP.net Calendar
is there a way to format date in asp.net to get only date eg 04/12/12

DMo
2012-04-24
re: Cool Tricks With The ASP.net Calendar
Something like
Protected Sub Calendar1_DayRender(sender As Object, e As System.Web.UI.WebControls.DayRenderEventArgs)

e.Cell.Text = e.Day.Date

End Sub

KING PACKING & REMOVAL COMPANY
2012-04-27
Moving House, moving house company
Royal package moving sports company to serve first, seeking
customers peace of mind plus service operation with the company
document, moved house, moved factory, move capital/f service. Our
company

Amjad
2012-05-03
re: Cool Tricks With The ASP.net Calendar
I want to display the number of request received each day in the selected month using calender control


1 (10) 2 (5) 3 (0)
where 1, 2 & 3 are the days of the selected month and 10, 5 & 0 are the number of request received on that specific days.

Please help

tharif
2012-05-03
re: Cool Tricks With The ASP.net Calendar
hai how can change year in the calender please help me

Ros
2012-06-25
re: Cool Tricks With The ASP.net Calendar
I want to increase the size of the clickable area on the days - by default only the text is clickable.

sandeep
2012-06-27
re: Cool Tricks With The ASP.net Calendar
hello, i want to show a different color for my calender dates, where the dates has been selected from data base (my sql). so it has to pick a date from data base and the selected dates has to been shown on my asp.net calender with the help of c#, so please help me out...

DL
2012-08-10
re: Cool Tricks With The ASP.net Calendar
To keep the selected day color:
If e.Day.Date = sender.selecteddate.date Then
e.Cell.Attributes.Add("onmouseout", onmouseoutStyle.Replace("@BackColor", Drawing.ColorTranslator.ToHtml(sender.SelectedDayStyle.backcolor)))
Else
e.Cell.Attributes.Add("onmouseout", onmouseoutStyle.Replace("@BackColor", rowBackColor))
End If

cwalker
2012-08-27
re: Cool Tricks With The ASP.net Calendar
You saved my life.Thanks alot man.Keep up the good work