CalendarExtender + Validator and DataBinding Issues

Last post 02-08-2007 11:52 PM by Harshal Chavan. 5 replies.

Sort Posts:

  • CalendarExtender + Validator and DataBinding Issues

    01-31-2007, 11:31 AM
    • Loading...
    • James_2JS
    • Joined on 02-13-2006, 3:21 PM
    • Posts 335

    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) Smile

    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...

    1. Enter a property (e.g. EnsureValidDate="true") on the CalendarExtender which prevents the user enter random text in the textbox
    2. Enter a list of valid input formats that the textbox can accept (e.g. dd MMMM yyyy, dd/MM/yyyy, dd-MMM-yyyy etc)
    3. 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

  • Re: CalendarExtender + Validator and DataBinding Issues

    02-02-2007, 8:48 AM
    • Loading...
    • James_2JS
    • Joined on 02-13-2006, 3:21 PM
    • Posts 335

    Bump. Anyone got any ideas on this?? MS Guys maybe?? Thanks, James

  • Re: CalendarExtender + Validator and DataBinding Issues

    02-08-2007, 4:43 AM
    • Loading...
    • James_2JS
    • Joined on 02-13-2006, 3:21 PM
    • Posts 335

    BUMP! One last try... can anyone help??

  • Re: CalendarExtender + Validator and DataBinding Issues

    02-08-2007, 8:04 AM

    Hi

    u have to set the textbox readonly=true so the user cant enter anything wrong

    another is in textbox u can set onfocus="this.blur();" 

    required filed validator is then enough

    for binding the value u have to cast the database value in the format which is used in calender extender

    it should be done from code behind 

  • Re: CalendarExtender + Validator and DataBinding Issues

    02-08-2007, 11:59 AM
    • Loading...
    • James_2JS
    • Joined on 02-13-2006, 3:21 PM
    • Posts 335

    Thanks Harshal but....

    "I also want them to be able to enter a date manually."

    It is always quicker to type in a date... not to have to click several times to get the desired value. Hence not using readonly=true!!!!

  • Re: CalendarExtender + Validator and DataBinding Issues

    02-08-2007, 11:52 PM

    u can use both required field and regular expression validator

    and the expression will a date in mm/dd/yyyy format, rearrange the regular expression to

    (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d .

    For dd-mm-yyyy format, use (0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d .

Page 1 of 1 (6 items)
Microsoft Communities
Page view counter