Sometimes you need to dig in a little to get things to work the way that you want.
I’m working on an events calendar and I need specific behavior for the home page display.
When the page initially renders, I want to display a list of events in the current month.
Then, I want to modify the display based on the user’s interaction.
- If the user picks a date then I want to display only the events on that day.
- If the user navigates to a different month I want to display the events for that month, even if they previously choose a specific day.
To do this I need to do the equivalent of a SQL SELECT based on EITHER the Day or the Month depending on the users choices.
The code for this was easy but figuring out exactly what to do in relation to the controls behavior took a few minutes to straighten out.
The Calendar Control has a SelectedDate property but until a user chooses a specific DAY it’s values is 01/01/0001. (Meaning if you evaluate the properly in the Load Event it has not been set to anything.)
Clicking on the next or previous month links do not change the SelectedDate property. If the user never selected a date it’s still 01/01/0001 and if the user has previously selected a date, changing the current month has no effect on the SelectedData property either.
It’s easy to understand why this is the case if you view the Calendar Control as a “Date Picker”.
My answer came in leveraging the various events thrown by the control.
The Calendar has a VisibleDate property that contains a DateTime object who’s value is set to the first day of the Month being displayed in the calendar control. This property is updated whenever the user changes the month whether they have selected a specific day or not.
Unfortunately, the VisibleDate property is not set by default. (Meaning if you evaluate the properly in the Load Event it has not been set to anything.)
Here is the simple code I’m using to get the date “type” (Month or Day) and the value to use for the SQL Parameter based on the state of the control and the user’s interaction.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace CalendarDrivenSelect
- {
- public partial class _Default : System.Web.UI.Page
- {
- DateTime usableDate;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- usableDate = SelectCalendar.TodaysDate;
- TextBox1.Text = usableDate.Month.ToString();
- }
- }
- protected void SelectCalendar_SelectionChanged(object sender, EventArgs e)
- {
- usableDate = SelectCalendar.SelectedDate;
- TextBox1.Text = usableDate.ToString();
- }
- protected void SelectCalendar_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
- {
- usableDate = SelectCalendar.VisibleDate;
- TextBox1.Text = usableDate.Month.ToString();
- }
- }
- }
If the page is not a postback, I use the Control’s TodaysDate value then whenever the user makes a change I take action in the event handler for the “type” of change that they make. If they select a specific date, I extract that date. If they later select a different month to display I extract that Month and that selection overrides the previously selected date.
In the code above I’m just populating a text box but in the application code I will use that value to set the actual Events SELECT criteria.
A simple solution but not immediately obvious.
Hope it helps someone.






















RE: Coaxing the Calendar Control – Which should have been called the Date-Picker Control.
Pingback from Twitter Trackbacks for Coaxing the Calendar Control ??? Which should have been called the Date-Picker Control. : Misfit Geek [msjoe.com] on Topsy.com
RE: Coaxing the Calendar Control – Which should have been called the Date-Picker Control.
Pingback from Coaxing the Calendar Control – Which should have been called the Date-Picker Control. : Misfit Geek
Hi Joe,
I had to do something like this recently:
http://www.visityubasutter.com/…/Events.aspx
I used range validators on the form and start and end date params for the TSQL queries. I put a ListView in a TabContainer from the AJAX Control Toolkit – actually used one of your video tutorials to get started with that. My AJAX skills were not up to what I would have liked this to be, but it works!
Anthony
RE: Coaxing the Calendar Control – Which should have been called the Date-Picker Control.
Thank you for submitting this cool story – Trackback from DotNetShoutout
Joe, could you post the full code – aspx .cs files? I’m new to asp.net and am learning by example.
Thanks.
great info, very helpful.
Hej Joe;
Great stuff all around, videos, codes and blog.
Actually I would like to know if you had this problem while debugging a website application that used a 3.0 vers of AJAXtoolkit with the new 3.5 vers.
The debugger stops at this level:
throw new Error("AjaxControlToolkit requires ASP.NET Ajax 4.0 scripts. Ensure the correct version of the scripts are referenced. If you are using an ASP.NET ScriptManager, switch to the ToolkitScriptManager in AjaxControlToolkit.dll.");
So why the new one is capable of processing the old AJAX controls scripts?
The AJAX 4.0 files have this vers. properties 3.5.404122.
I am not sure if that was clear enough, but I know you da man, so Can you help?
Hello once more,
If you don’t mind, I have added a link to misfitgeek on my site.
is that ok by you?
Hey Joe,
"whatcha gonna do with that gun in hour hand"
I bet you heard this one before, but I found the solution to the Toolkit problem with the 3.0 version.
All I had to do was insert the rgistration on each page I am using the new kit like this
<%@ Register
Assembly="AjaxControlToolkit"
Namespace="AjaxControlToolkit"
TagPrefix="ajaxToolkit" %>
And use this line instead of the old scriptmanager line like this
<ajaxToolkit:ToolkitScriptManager runat="server" ID="ScriptManager1" />
and for good sense sakes NOT to copy it to the VS /common/IDE folder.
That’s it, pass it on.
Again thanx for all those wonderfull and helpfull videos, I use them to get hints on how to do a few tricks of my own.
Truly,
Bas