カレンダーにイベントを表示するための次のコードがあります
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
ただし、これは1日に複数のイベントがない場合は正常に機能しますが、1日に複数のイベントがある場合、つまり'scheduleData.Add(New DateTime(2011、2、15 )、 "Go Shopping")はコメントされていません
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.
親切に私を助けてください。