Using the ASP.NET calendar control to display event information

The calendar control is useful for displaying event dates that users can then click on for more information. Here are the steps for setting up the calendar control to display event information from database.

1. Place a calendar control on the required page (for example, default.aspx)

2. Create a second page called calendar.aspx for displaying the event information.

3. In your codebehind file for the page with the calendar control (default.aspx.vb in my example), create the SelectionChanged Sub for your calendar control (calendar1 in my example) and enter

  Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
    ' Display calendar details when date selected
    Dim eventDateSelected As String
    eventDateSelected = Format(Calendar1.SelectedDate.Date, "MM/dd/yyyy").ToString
    Response.Redirect("calendar.aspx?eventDate=" & eventDateSelected)
  End Sub

The code above retrieves the date clicked on by the user (Calendar1.SelectedDate.Date), formats the date, and redirects the user to calendar.aspx with the date selected as a querystring. You will need to format the date to match the format of the date in your database events table.

4. In calendar.aspx, use a data control such as a detailsview or formview to display the information.

5. When setting up your formview or detailsview data source (assuming that you are using Visual Studio or Visual Web Developer), click the Where button to create the querystring criteria. The column should be your date column, the Source should be QueryString, and the QueryString Field should be eventDate (if you are using the code above).

One thing I should stress is that the date format from the database needs to match the date format of the query string. I use a SQL Server 2005 database to store event data. In the view I created to display event information, I use the following conversion to extract the date from the eventDate column and match it with the query string:

CONVERT (nvarchar(30), eventDate, 101)

101 is one of the predefined date formats.

Published Friday, March 09, 2007 3:51 AM by gcastner

Comments

No Comments