我有以下代码在日历上显示事件
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.
请帮助我。
答案 0 :(得分:1)
使用词典,您不能拥有两个具有相同键的条目。字典基于具有唯一键的[Key,Value]概念。
将此视为韦伯斯特的字典。您不会定义同一个单词(键)的两个不同实例,而是在该单词下面有多个定义(值)。
我建议添加时间元素,否则您可能需要使用不同的数据结构。