This article relates to a couple of articles that are on the site. The original article was "Cool Tricks With The ASP.net Calendar". You can find this here: http://weblogs.sqlteam.com/jhermiz/archive/2007/12/10/Cool-Tricks-With-The-ASP.net-Calendar.aspx. The other article you might find useful is the "Additional Tip For that Calendar Control In ASP.net" this article can be found here: http://weblogs.sqlteam.com/jhermiz/archive/2007/12/11/Additional-Tip-For-That-Calendar-Control-In-ASP.net.aspx.
Have you ever wanted to create a nice calendar control much like the sharepoint calendar control? You know the calendar control with hyperlink data directly on each calendar day. The previous articles shows you how to use the DayRender Calendar control event to add image controls. But what about adding hyperlink controls as well as setting the NavigateURL property for each hyperlink to take you to another web page.
This process is very similiar to the second article where we added image controls to a calendar control. Now we want to simply add hyperlink controls.
To do this drag and drop a calendar control on your web page. Use your SQL knowledge to pull some data (with the date for each row of data) from your database table. Store this data in a datatable or a dataset. Then loop through your data and compare the date in your dataset / datatable with the day in the Calendar_DayRender event. If they are equal instantiate a Hyperlink object. Set its text property, its navigateurl property and whether or not you want a border.
From our previous examples here's some code to help you out:
Protected Sub Calendar13_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar13.DayRender Dim nextDate As DateTime Dim hl As HyperLink 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 e.Cell.ForeColor = System.Drawing.ColorTranslator.FromHtml("#24618E") notTouched = False End If Next End If If Not CType(Session("dsRequests2"), DataSet) Is Nothing Then For Each dr As DataRow In CType(Session("dsRequests2"), DataSet).Tables(0).Rows nextDate = CType(dr("VacationDate"), DateTime) If nextDate = e.Day.Date Then hl = New HyperLink hl.Text = CType(dr("ToolTip"), String) hl.NavigateUrl = "Entry.aspx?ID=" & CType(dr("VacationID"), String) hl.BorderColor = Color.Gray hl.BorderWidth = 1 hl.ToolTip = "Click this item to get more detailed information." hl.Font.Name = "Arial Narrow" hl.Font.Bold = False hl.ForeColor = System.Drawing.ColorTranslator.FromHtml("#24618E") e.Cell.BackColor = Color.Azure e.Cell.Controls.Add(hl) 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 result: