ASP.net事件日历显示日期范围

时间:2012-02-15 10:24:28

标签: asp.net vb.net calendar date-range

我有一个日历,用于突出显示数据库中选定的日期。它目前仅突出显示开始日期...我希望它突出显示日期范围(开始日期 - 发送日期)

我目前的代码如下:

Function GetCurrentMonthData(ByVal startdate As DateTime, _
                              ByVal enddate As DateTime) As DataSet
    Dim dsMonth As New DataSet
    Dim cs1 As ConnectionStringSettings
    cs1 = ConfigurationManager.ConnectionStrings("ConnectionString1")
    Dim connString As String = cs1.ConnectionString
    Dim dbConnection As New SqlConnection(connString)
    Dim query As String
    query = "SELECT holidaydate FROM HolidayRequests WHERE HolidayDate >= @startDate AND HolidayDate < @endDate"

    Dim dbCommand As New SqlCommand(query, dbConnection)
    dbCommand.Parameters.Add(New SqlParameter("@startdate", startdate))
    dbCommand.Parameters.Add(New SqlParameter("@enddate", enddate))

    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 System.Web.UI.WebControls.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.Pink
            End If
        Next
    End If
End Sub

我认为使用的查询和where语句没有提取正确的信息,但不完全确定在何处或如何纠正它......

1 个答案:

答案 0 :(得分:0)

DayRender Sub需要更改如下:

Protected Sub Calendar1_DayRender(ByVal sender As Object, _
        ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) _
        Handles Calendar1.DayRender
    Dim nextDate As DateTime
    Dim StartDate As DateTime
    Dim Enddate As DateTime
    Dim username As String

    If Not dsHolidays Is Nothing Then
        For Each dr As DataRow In dsHolidays.Tables(0).Rows
            StartDate = CType(dr("StartDate"), DateTime)
            Enddate = CType(dr("EndDate"), DateTime)
            nextDate = CType(dr("startdate"), DateTime)
            username = CType(dr("username"), String)

            If e.Day.Date >= StartDate And e.Day.Date <= Enddate Then


                e.Cell.BackColor = System.Drawing.Color.Pink
            End If
        Next
    End If
End Sub