Click here to Skip to main content
Click here to Skip to main content
Go to top

Highlighting Important Dates in Calendar

, 9 Aug 2004
Rate this:
Please Sign up or sign in to vote.
Highlighting important dates in the Calendar control.

Introduction:

We all are familiar with the ASP.NET Calendar control. We can click on dates and it will be highlighted. But once you refresh the page, the selected dates are gone. How about making a small application that remembers the date you selected.

Creating a Simple Calendar:

For creating a calendar, simply drag and drop the Calendar control from the ToolBox usually located on the left in VS.NET IDE. Once you get the Calendar control on the page, you can change the appearance of the calendar by right clicking on it and choosing Properties, and then Auto Format. The image below shows just a calendar highlighting current date.

Sample screenshot

Making Database Table for storing selected Dates:

In this example, we also need to create a database. I made the database in SQL Server 2000. To keep this simple, I only made two fields: one is CalendarID which is an autogenerating number and the other is DateTime.

Page_Load Event Code:

private void Page_Load(object sender, System.EventArgs e)
{
  if(!Page.IsPostBack)
  {
    // Calls the Bind Method
    BindData(); 
  }
}

BindData Method Code:

public void BindData()
{
  SqlCommand myCommand = new SqlCommand("SELECT CalDate FROM tblCal", 
                                                        myConnection);
  myCommand.CommandType = CommandType.Text;
  // Opens a Database Connection
  myConnection.Open();
  // Execute DataReader
  SqlDataReader dr = myCommand.ExecuteReader();
  // Read DataReader till it reaches the end
  while( dr.Read() == true )
  {
    // Assign the Calendar control dates
    // already contained in the database
    myCal.SelectedDates.Add((DateTime)dr.GetSqlDateTime(0));
  }

  // Close DataReader
  dr.Close();
  // Close database Connection
  myConnection.Close(); 
}

Mark Test Dates Button Event Code:

private void Button1_Click(object sender, System.EventArgs e)
{
  // Binds the Data on the page
  BindData(); 
  // Set the color of Selected Calendar Cells to Red
  myCal.SelectedDayStyle.BackColor = System.Drawing.Color.Red;

  SqlCommand myCommand = new SqlCommand("InsertDate",myConnection);
  myCommand.CommandType = CommandType.StoredProcedure;
  myCommand.Parameters.Add(new SqlParameter("@v_DateTime",SqlDbType.DateTime));
  myCommand.Parameters["@v_DateTime"].Value = selectedDate;
  myConnection.Open();
  myCommand.ExecuteNonQuery();
  myConnection.Close();
}

Once you press the button, your calendar will appear like the image below showing the selected dates:

Sample screenshot

Loading the page a second time:

You can also change the color when the page loads a second time as seen in the image below in which the calendar is loaded a second time:

Sample screenshot

Thanks and enjoy coding!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

azamsharp
Web Developer
United States United States
I am the founder of knowledge base website, HighOnCoding, GridViewGuy, RefactorCode.com and ScreencastADay.com.
 
HighOnCoding is a website which will get you high legally with useful information. There are tons of articles, videos and podcasts hosted on HighOnCoding.
 
HighOnCoding.com www.HighOnCoding.com
 

My Blog:

Blog

 

Buy my iPhone app ABC Pop

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberhims05629-Nov-12 18:34 
GeneralDisplay all the months with the event highlighted for any given year Pinmemberdoran_doran5-May-09 13:11 
QuestionEverytime page refreshes, calander does not display highlighted dates - why? Pinmemberdimithrak2-Apr-08 4:28 
AnswerRe: Everytime page refreshes, calander does not display highlighted dates - why? PinmemberJasonC7213-Oct-08 5:17 
GeneralRe: Everytime page refreshes, calander does not display highlighted dates - why? Pinmemberazamsharp13-Oct-08 5:35 
Check out the following link which uses Ajax:
 
http://www.gridviewguy.com/ArticleDetails.aspx?articleID=232
 
When the page refreshes you need to load the dates again from SQL SERVER or whatever your persistent storage is
 
Mohammad Azam
azamsharp@gmail.com
www.gridviewguy.com
www.screencastaday.com
Houston, TEXAS

GeneralAzam or anyone.. please help Pinmemberalexammu25-Jul-06 14:28 
QuestionHow to use more than two colors Pinmembergopalvenus5-May-06 10:11 
GeneralEh, this could be done better. Such as... Pinmemberdaerath6-Apr-05 11:54 
GeneralCalendar control previous months Pinmemberams14322-Dec-04 0:34 
GeneralCalendar control PinmemberCriticalCynic8-Nov-04 7:44 
GeneralRe: Calendar control Pinmemberazamsharp10-Nov-04 9:06 
GeneralSource code missing PinmemberTony Bermudez10-Aug-04 7:57 
GeneralRe: Source code missing Pinmemberazamsharp11-Aug-04 11:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 10 Aug 2004
Article Copyright 2004 by azamsharp
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid