Populating Form Inputs Using the Calendar Control, Part 2
By James Avery
In Part 1 we looked at how to create a general calendar control and explained the aim of this article: to create a calendar pop-up to facilitate selecting a date for a form field text box. In this part we'll complete the application by examining how to "beautify" our calendar control and add the essential functionality.
Making the Calendar Control Pretty!
Since by default there is no selected date on the calendar we are going to capture the
OnDayRender event so we can change the background color of the
current date to a different color. We could also change the font size,
or make the date bold if we wanted to. We can easily accomplish this by adding
an event handler to our control like so:
<asp:Calendar id="id" runat="server"
|
Here the EventHandler is the name of the evant handler that you must provide
to handle the OnDayRender event. Note that for my application I created a
function named Caledar1_dayrender to override handle this event.
There are a few more properties we may wish to set to make the calendar control more useful.
One important one is to set the SelectionMode to Day, which limits the user
to selecting a single day as opposed to a range of dates. There are a number of UI/stylistic properties
you may wish to set as well, such as BorderColor, BackColor,
DayNameFormat, TitleStyle, etc. You should consult the documentation for more information on the
properties of the calendar Web control.
At this point, we have created a calendar control, set some properties, and specified an event handler for
the OnSelectionChanged and OnDayRender events.
|
Note that we still need to add code for our Calendar1_SelectionChanged event handler and
Calendar1_DayRender method. Let's first look at the Calendar1_DayRender method.
We simply want to use this method to highlight the current date when the user loads the
calendar. To accomplish this, we simply need to add these three lines of code:
|
Notice that we use the properties Day and Cell which are
made available through the DayRenderEventArgs object. The Cell property
represents the cell around the date and the day property is the date being rendered.
Now all that remains is the Calendar1_SelectionChanged event handler, where we need to
emit client-side JavaScript code to update the form and close the pop-up. While we could simply use
Response.Write's, the output would appear before any other HTML content (including
the <html> tag). So, to make our HTML "correct," we'll use a literal control
inbetween the <head> ... </head> tags. Creating this literal control is easy
enough:
<head>
|
We will use this literal control to hold our JavaScript after we create it.
Now lets look at creating the JavaScript to pass the value back to the form. The first thing
we will do is create a string, strJScript, to hold our JavaScript code. Next, we will
build up this string so that it's end contents set the form's text box's value to the selected date
and the window is closed. This is accomplished with the following code in the
Calendar1_SelectionChanged event handler.
|
Here we round out the event handler by setting the text of our literal to the dynamically generated client-side JavaScript code. This has the effect of executing this client-side JavaScript code when the page is rendered. So when a user selects a date this event will fire and the JavaScript will be created. As the page is rendered the JavaScript will be run, the value of the selected date transferred to the form's text box, and the calendar page closed. Neat!
Happy Programming!
Attachments
calendar.aspx in VB.NET codecalendar.aspx in C# code




