I am going to write a new series of blog posts regarding the most common web server controls we use in ASP.Net applications.Lots of people that start now in ASP.Net want to know more about these standard controls.
In this post I will look into the calendar control. I will present its basic functionality and also look into the more advanced functionality it has.
I will be using VS 2010 Ultimate edition and C# to create a simple asp.net application.
1) Launch Visual Studio 2010/2008/2005.
2) Create a new website and name it with a meaningful name, e.g CalendarSite
3) Drag and drop a Calendar control from the toolbox into the default.aspx page.Leave the default name
4) Let’s examine some of the most used properties.
SelectedDate: By setting this property to a value, you have this value-date selected in the calendar control. Set the value to a date and run your application
VisibleDate: Whenever we want to use a different month or year, when the control initially loads, we can set the VisibleDate property to a date
If you want to select any particular day just click on the hyperlink on the bottom of each day.
5) But what if we wanted to select all of the days of a particular week or all the days of a particulat month.
Then we need to look into another useful property of the Calendar control, the SelectionMode. For selecting all the days of the week, you can set its value to DayWeek.
For selecting all the days in a month, you can set its value to DayWeekMonth.
Run your application and you will see more selectors (arrows) in the calendar control. By clicking on them you will be able to select a whole week or month.
Some other very important properties are:
DayNameFormat , which allow us to change the number of letters the header day text contains
FirstDayOfWeek, which allow us to set the first day of the week to Monday if we want to
We have so many options to style the control by setting the values of properties like BorderColor,BordeWidth,BorderStyle,BackColor to the values we want.
We can also set the values of various font properties. We can always use the smart tag(little arrow on the top right hand corner) to use one of the many predefined formatting styles for our calendar control.
Now that I have covered some of the basic stuff, I would like to mention
Calendar click events
Control Calendar cell’s rendering
6) Every time the user selects a date in you calendar control, you capture this selection by the SelectionChanged event.
Add a literal control to the default.aspx page. Name it SelectedDatesLiteral
Just double click on a day in the calendar control. In the event handling routine, type the following
Here, I just loop through the selected dates and append their values to a literal control.Run your application and see for yourself.
If we want to add content in a day in the calendar, we must use the DayRender event. This event is invoked once for every day in the calendar. The table cell and the date are passed to the event handler and can be used to render custom content or look up data in a database etc. We will pull event data from an xml file.Add an xml file to your project and call it events.xml. Place this file in the App_Data special folder. The contents of this file could be something like this.
Add a XMLDataSource control in to your default.aspx page and configure it so it reads the contents of your xml file.
Then in the DayRender Event type the following
Make sure you have added these namespaces in the Default.aspx.cs file
I am using an XPath to filter out only the nodes in our xml document that have content.
Then I am opening the xml document and then I am building a string with values that will be displayed in the calendar cell for the current day.
Then I loop through all the items that were returned back from the XML document and append those values together in order to create a string of all of events that happend in this specific day.
Run your application and see the content that is passed from the xml file to the specific dates, e.g Christmas day.
If you need the full code, just email me.
Hope it helps!!!