0

我有以下代码在日历上显示事件

Private scheduleData As New Dictionary(Of DateTime, String)(5)

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        scheduleData.Add(New DateTime(2011, 1, 9), "Vacation Day")
        scheduleData.Add(New DateTime(2011, 1, 18), "Budget planning meeting")
        scheduleData.Add(New DateTime(2011, 2, 5), "Conference call")
        scheduleData.Add(New DateTime(2011, 2, 10), "Meet with art director")
        scheduleData.Add(New DateTime(2011, 2, 15), "Vacation day")
        'scheduleData.Add(New DateTime(2011, 2, 15), "Go Shopping")
        Calendar1.Caption = "Personal Schedule"
        Calendar1.FirstDayOfWeek = System.Web.UI.WebControls.FirstDayOfWeek.Sunday
        Calendar1.NextPrevFormat = NextPrevFormat.ShortMonth
        Calendar1.TitleFormat = TitleFormat.MonthYear
        Calendar1.ShowGridLines = True
        Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Left
        Calendar1.DayStyle.VerticalAlign = VerticalAlign.Top
        Calendar1.DayStyle.Height = New Unit(75)
        Calendar1.DayStyle.Width = New Unit(100)
        Calendar1.OtherMonthDayStyle.BackColor = System.Drawing.Color.Cornsilk
        Calendar1.TodaysDate = New DateTime(2011, 1, 1)
        Calendar1.VisibleDate = Calendar1.TodaysDate
    End Sub

和日期渲染处理程序

Protected Sub Calendar1_DayRender(sender As Object, e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        If scheduleData.ContainsKey(e.Day.[Date]) Then
            Dim lit As New Literal()
            lit.Text = "<br />"
            e.Cell.Controls.Add(lit)
            Dim lbl As New Label()
            lbl.Text = DirectCast(scheduleData(e.Day.[Date]), String)
            lbl.Font.Size = New FontUnit(FontSize.Small)
            e.Cell.Controls.Add(lbl)
        End If

    End Sub

但是,如果我一天没有超过一个事件,但如果一天中有多个事件,我会收到下面的错误,即如果显示为 'scheduleData.Add(New DateTime(2011, 2, 15) 的行), "去购物") 未注释

An item with the same key has already been added.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: An item with the same key has already been added.

请帮帮我。

4

1 回答 1

1

使用字典,您不能有两个具有相同键的条目。字典基于具有唯一键的 [Key, Value] 概念。

把它想象成韦氏词典。您不会定义同一个词(键)的两个不同实例,而是在该词下有多个定义(值)。

我建议添加一个时间元素,或者您可能必须使用不同的数据结构。

于 2012-06-14T14:31:50.497 回答