Hi everyone,
Forgive me if I've missed an easy trick here, but I'm struggling a bit with the Calendar extender with respect to validation. Here's my problem:
I'm creating some input forms for my users where they must pick a date. I'm loving the calendar's look and feel so want to give that to my users... but I also want them to be able to enter a date manually. So I've got a textbox for the date entry and a calendar extender and to make things clear for the user, I've set the Format to be dd MMMM yyyy.
<asp:TextBox ID="DateTextBox" runat="server" /><ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="DateTextBox" Format="dd MMMM yyyy" />
I can add a required field validator to ensure the user enters something:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DateTextBox" ErrorMessage="Enter the date please">*</asp:RequiredFieldValidator>
But then I start hitting problems!!! I now want to make sure that the user enters a valid date (the user can currently enter anything they want here)... so I add in a CompareValidator:
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="DateTextBox" Operator="DataTypeCheck" Type="Date" ErrorMessage="Not a valid date">*</asp:CompareValidator>
But that doesn't like the dates selected from the calendar, because they're not in the format the CompareValidator expects (dd/mm/yyyy, mm/dd/yyyy or yyyy/mm/dd - I think) 
What's more, when I enter a valid (something the CompareValidator likes) date e.g. 01/01/2007 the calendar then can't work out what the selected date is (i.e. it doesn't highlight 1st Jan!!).
I can fix the first of these problems with a CustomValidator using server-side code and Date.Parse... but that doesn't have the instantaneous response of the client side code, requiring a postback. It also doesn't deal with the second problem of recognising the selected date!!
I also want to store and retrieve this data from a datbase. So I add in a Text='<%# Bind("DateField") %>' attribute on my TextBox. This is great for storing the data in the database, but when it retrieves it back to the client, it renders it as 01/01/2007 00:00. This really doesn't look good for the user!!
Again, I can overcome this by handling the DataBound event for the parent control on the server, finding the TextBox and reformatting it's value. But that's really messy and a pain to do every time I want to put this on a form!
So here's what I really want to be able to do...
- Enter a property (e.g. EnsureValidDate="true") on the CalendarExtender which prevents the user enter random text in the textbox
- Enter a list of valid input formats that the textbox can accept (e.g. dd MMMM yyyy, dd/MM/yyyy, dd-MMM-yyyy etc)
- When I set the Format property it is used to display data from the Bind call
Am I asking too much, can anyone offer an alternative suggestion to accomplish what I'm after, or should this be a feature of the control??
Thanks,
James