Calendar Control DayRender

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tigers07
    New Member
    • Aug 2007
    • 3

    Calendar Control DayRender

    I've been banging my head on a problem all day and am looking for some advice. I'm fairly new to asp.net/vb.net so I appologize if this seems a bit elementary. Anyway, what I'm doing is displaying 12 calendar controls on a page and attempting to update them with holiday and eventually vacation information.

    The methods SetCalendarValu es() and GetCurrentMonth Data() work as expected, however, I'm not quite sure how to incorporate the method that utilizes dayrender to modify the calendar - Calendar1_DayRe nder. Is it possible to use the DayRenderEventA rgs outside of the Calendar.DayRen der method?

    Any advice would be greatly appreciated.


    Code:
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Web.UI.WebControls
    
    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected dsHolidays As DataSet
    
        Protected Sub Page_Load(ByVal sender As Object, _
                ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                SetCalendarValues()  
            End If
        End Sub
    
        Protected Sub SetCalendarValues()
            Dim dt As DateTime
            Dim cal As Calendar
            Dim MonthNumber, YearNumber As Integer
    
            For i As Integer = 1 To 12
                dt = New DateTime(DateTime.Now.Year, i, 1)
                cal = DirectCast(Me.FindControl("Calendar" & i.ToString()), Calendar)
                cal.VisibleDate = dt
    
                Dim firstdate = New DateTime(cal.VisibleDate.Year, _
                                         cal.VisibleDate.Month, 1)
    
                If cal.VisibleDate.Month = 12 Then
                    MonthNumber = 1
                    YearNumber = cal.VisibleDate.Year + 1
                Else
                    MonthNumber = cal.VisibleDate.Month + 1
                    YearNumber = cal.VisibleDate.Year
                End If
                Dim lastdate = New DateTime(YearNumber, MonthNumber, 1)
    
                dsHolidays = GetCurrentMonthData(firstdate, lastdate)
    
            Next
        End Sub
    
    Function GetCurrentMonthData(ByVal firstDate As DateTime, ByVal lastDate As DateTime) As DataSet
            Dim dsMonth As New DataSet
            Dim cs As ConnectionStringSettings
            cs = ConfigurationManager.ConnectionStrings("ConnectionString1")
            Dim connString As String = cs.ConnectionString
            Dim dbConnection As New SqlConnection(connString)
            Dim query As String
            query = "SELECT HolidayDate FROM Holiday " & _
                " WHERE HolidayDate >= @firstDate AND HolidayDate < @lastDate"
            Dim dbCommand As New SqlCommand(query, dbConnection)
            dbCommand.Parameters.Add(New SqlParameter("@firstDate", firstDate))
            dbCommand.Parameters.Add(New SqlParameter("@lastDate", lastDate))
    
            Dim sqlDataAdapter As New SqlDataAdapter(dbCommand)
            Try
                sqlDataAdapter.Fill(dsMonth)
            Catch
            End Try
            Return dsMonth
        End Function
    
    Protected Sub Calendar1_DayRender(ByVal sender As Object, _
                ByVal e As DayRenderEventArgs) _
                Handles Calendar1.DayRender
            Dim nextDate As DateTime
            If Not dsHolidays Is Nothing Then
                For Each dr As DataRow In dsHolidays.Tables(0).Rows
                    nextDate = CType(dr("HolidayDate"), DateTime)
                    If nextDate = e.Day.Date Then
                        e.Cell.BackColor = System.Drawing.Color.DimGray
                        Dim label As Label = New Label()
                        e.Cell.Controls.Clear()
                        label.Text = "H"
                        e.Cell.Controls.Add(label)
                    End If
                Next
            End If
        End Sub
    
    End Class
    Last edited by tlhintoq; Apr 3 '09, 09:33 PM. Reason: [CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • Tigers07
      New Member
      • Aug 2007
      • 3

      #3
      Cool - thanks for cleaning that up. I'll check them out and use them from now on.

      Comment

      Working...