I'm trying to modify Munir Shaikh's August 21, 2007, calendar article just a bit to make it an event calendar. The calendar is populated through a database with short titles in each day's box. But, when the number is clicked upon, I'd like the details for that day displayed below the calendar. I thought I had it working, but I'm having trouble. In the code below, I've done a response.write when the date is passed to the calList_DayRender sub and it appears correct. Then I wrote it again after it passes within the sub and it appears correct. I wrote each date from the dataset and they appear to correlate. Can someone help me? Many thanks. (Sorry, it's in vb)
<asp:Calendar ID="calList" runat="server" OnSelectionChanged="chgSelect" /><asp:label ID="lblDetails" runat="server" />
Inherits System.Web.UI.Page Public strConn As String = ConfigurationManager.ConnectionStrings("strConn").ConnectionString Public cn As New Data.SqlClient.SqlConnection(strConn) Public da As New Data.SqlClient.SqlDataAdapter Public ds As New Data.DataSet Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load da = New Data.SqlClient.SqlDataAdapter("wnchn.dbo.pCalendar 13,0, '','','','',''", cn) da.Fill(ds, "Events") lblDetails.BackColor = System.Drawing.ColorTranslator.FromHtml("#E3E9C3") lblDetails.ForeColor = System.Drawing.ColorTranslator.FromHtml("#558497") lblDetails.Font.Size = "10" End Sub Protected Sub calList_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles calList.DayRender 'Declare Vars Dim sb As New StringBuilder Dim alt As Boolean = True Dim dr As Data.DataRow 'Step through the rows returned from the database. For Each dr In ds.Tables("Events").Rows Dim edate As Date = dr.Item("start_date") If edate.ToShortDateString = e.Day.Date.ToShortDateString Then If alt = True Then sb.Append("<tr><td></td><td>") alt = False Else sb.Append("<tr><td></td><td>") alt = True End If sb.Append("</a><span class='small'>") sb.Append(dr("short_Title")) sb.Append("</span></td></tr>") End If Next 'Insert top table tag in the string builder sb.Insert(0, "<div align=center><table cellpadding=0 cellspacing=0 border=0 width=100%>", 1) 'Append bottom table tag in the string builder sb.Append("</table></div>") 'Add a new literal control to the day containing the text of the event. e.Cell.Controls.Add(New LiteralControl(sb.ToString)) If e.Day.Date.ToShortDateString = Today().Date.ToShortDateString Then getDetails(e.Day.Date.ToShortDateString) End If End Sub Sub chgSelect(ByVal sender As Object, ByVal e As EventArgs) getDetails(calList.SelectedDate.ToShortDateString) End Sub
Sub getDetails(ByVal myDate As Date) Dim sb As New StringBuilder Dim alt As Boolean = True Dim dr As Data.DataRow 'Step through the rows returned from the database. For Each dr In ds.Tables("Events").Rows If Convert.ToDateTime(dr.Item("start_date")).ToShortDateString = myDate.ToShortDateString Then sb.Append(" ") sb.Append(dr("myDetails")) sb.Append(" <br />") End If Next If sb.ToString = String.Empty Then lblDetails.Text = " No items scheduled for this date. " Else lblDetails.Text = sb.ToString End If End Sub