Brian Mains

Catch me on linked in at: http://linkedin.com/in/brianmains, or follow me on twitter at: @brianmains.

Intro to Telerik MVC's DatePicker Control

The freely available ASP.NET MVC framework by Telerik has added a new DatePicker control that works similarly to the RadDatePicker control in its ASP.NET AJAX framework.  Below is a sample of that control:

<%
    Html.Telerik().DatePicker().Name("Picker")
        .ButtonTitle("Select Date")
        .ClientEvents((evt) =>
            {
                evt.OnChange(() =>
                    {
                    %>
                        function(e) {
                            $("#output").html("Value changed to " + e.date);
                        }
                    <%   
                });
            })
        .Value(DateTime.Now)
        .MaxDate(DateTime.Now.AddYears(3))
        .MinDate(DateTime.Now.Subtract(new TimeSpan(1000, 0, 0, 0)))
        .Render();
%>

Note that this date picker has some of the same properties as the calendar in my previous post.  The MaxDate, MinDate, ClientEvents, etc are the same properties available in both, as you have access to a wide array of options available to you).  The value property allows the defaulting of the current date as I've currently set, as well as a tooltip for the button, which is shown as a tooltip in the image below.

Clicking on the button shows a calendar control.  Clicking on the date fires our client-side change handler.  Our handler writes out the selected date to the browser as is, which renders the option below.

Currently, the selected date is 4/13/2010; clicking on the 22nd will fire the change event, making available an e.date property with the new date.  By using the client-side equivalent handler, for both the date picker and calendar, you avoid the postback to the server to an action method, saving on resources.

Posted: Sat, Apr 10 2010 14:22 by bmains | with 5 comment(s)
Filed under: , ,

Comments

lydia said:

i test this compentents in my mvc project but the are some problems the popup button is disabled

# August 2, 2010 8:46 AM

bmains said:

Hmm, I didn't have any issue... you have the telerik script files in the project?  Do you see a javascript error?  What does the MVC control definition look like?

# August 13, 2010 7:40 AM

Thomas said:

Hi

Is there any way to get this control to show the week numbers and to start the week with Monday?

# August 18, 2010 7:05 AM

J Dubs said:

I do have a small problem with this control.

I'm using a DatePicker control inside of a popup window that is launched via an edit button within a child Telerik grid.

Initially, the time portion is shown but then disappears when I click anywhere in the window!

Any ideas on why this would be the case?

Here my standard shared DateTime.cshtml (I'm using MVC3):

@using Telerik.Web.Mvc.UI

@model DateTime?

@(Html.Telerik().DatePicker()

   .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(String.Empty))

   .HtmlAttributes(new { id = ViewData.TemplateInfo.GetFullHtmlFieldName(String.Empty) + "_wrapper" })

   .ShowButton(true)

   .Value(Model.HasValue ? Model : System.DateTime.Now)

   .Format("dd/MM/yyyy")

)

Bit stumped here?

# June 20, 2011 5:41 AM

J Dubs said:

Fixed it.

I was missing the annotation:

[DataType(DataType.Date)]

for my date fields.  Don't really know why this is required, but it seems to do the trick!

# June 20, 2011 8:09 AM
Leave a Comment

(required) 

(required) 

(optional)
 

(required) 

If you can't read this number refresh your screen
Enter the numbers above: