UI For ASP.Net MVC - DateTimePicker

Here is a tutorial to help you add a feature on the DatePicker and DateTimePicker controls UI for ASP.NET MVC of Telerik.
 
By default, these controls will not open when you click on the text field and there is no property to do so:

 

 


 
Here is how to add this feature.

1. ExtendedDatePicker

Create a new ExtendedDatePicker object that will allow extending the DatePicker and contain a new ShowOnClick property as in the following:
  1. public class ExtendedDatePickerBuilder : DatePickerBuilder  
  2. {  
  3.     /// <summary>  
  4.     /// Boolean that store show on click 
  5.     /// </summary>  
  6.     public bool ShowOnClick { getset; }  
  7.   
  8.     public ExtendedDatePickerBuilder(DatePickerBuilder datePicker)  
  9.         : base(datePicker)  
  10.     {  
  11.   
  12.     }   
  13. }  
2. HtmlExtensions
 
It will now act to create a HTML extension to extend our DatePicker and initialize the ShowOnClick:
  1. public static class HtmlKendoExtensions  
  2. {  
  3.     /// <summary>  
  4.     /// Expand the DatePickerBuilder Telerik to our custom ExtendedDatePickerBuilder  
  5.     /// </summary>  
  6.     /// <param name="datePickerBuilder"></param>  
  7.     /// <returns></returns>  
  8.     public static ExtendedDatePickerBuilder Extend(this DatePickerBuilder datePickerBuilder)  
  9.     {  
  10.         return new ExtendedDatePickerBuilder(datePickerBuilder);  
  11.     }  
  12.   
  13.     /// <summary>  
  14.     /// Set the ShowOnClick option to true  
  15.     /// </summary>  
  16.     /// <param name="datePickerBuilder"></param>  
  17.     /// <returns></returns>  
  18.     public static ExtendedDatePickerBuilder ShowOnClick(this ExtendedDatePickerBuilder datePickerBuilder)  
  19.     {  
  20.         datePickerBuilder.ShowOnClick = true;  
  21.   
  22.         return datePickerBuilder;  
  23.     }  
  24. }  
3. View (cshtml)
 
Now it is possible to extend in our views Kendo DatePicker and DatePickerFor controls with our extension.

File in your WebApp: Go to Views, Shared, then EditorTemplates  to create a Date.cshtml.
  1. @model DateTime?  
  2.   
  3. @(Html.Kendo().DatePickerFor(m => m).Extend().ShowOnClick())  
So just to extend the DatePicker, call our ShowOnClick() extension.
 
4. ToHtmlString()
 
Now to override the ToHtmlString() method to render in the HTML the DatePicker and add our JavaScript script that will open the datepicker in the browser to the click.

To do this, add the following method in our ExtendedDatePickerBuilder class as in the following: 
  1. #region overriding  
  2.   
  3. public override string ToHtmlString()  
  4. {  
  5.     StringBuilder sbShowOnFocus = new StringBuilder(" ");  
  6.   
  7.     if (ShowOnClick)  
  8.     {  
  9.         sbShowOnFocus.Append("<script type=\"text/javascript\">");  
  10.   
  11.         sbShowOnFocus.Append("$(document).ready(function () {");  
  12.         sbShowOnFocus.Append("    $(\"input[name='" + base.Component.Name + "']\").click(function() {");  
  13.         sbShowOnFocus.Append("        var datepicker = $(\"input[name='" + base.Component.Name + "']\").data(\"kendoDatePicker\");");  
  14.         sbShowOnFocus.Append("        datepicker.open();");  
  15.         sbShowOnFocus.Append("    });");  
  16.         sbShowOnFocus.Append("});");  
  17.   
  18.         sbShowOnFocus.Append("</script>");  
  19.     }  
  20.   
  21.     var mvcHtmlStringResult = MvcHtmlString.Create(sbShowOnFocus.ToString());  
  22.   
  23.     return base.ToHtmlString() + mvcHtmlStringResult.ToString();  
  24. }  
  25.  
  26. #endregion  
 
After the page is loaded, this script allows to add a subscription to a click on the control to open the DatePicker.
 
5. End ! 
 
With this method, you can add other properties on your controls.
 
When controls are loaded, a click on the control opens the DatePicker as in the following:

 



 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
The benefit of this method is to limit the impact on the JavaScript of the Kendo controls in your application and to be less dependent on third-party controls. 
 
Next, you can add other properties like ShowOnFocus or other methods on your control!
 
Note: Attention, with a direct legacy: ExtendedDatePickerBuilder: DatePickerBuilder, it is possible, after the ShowOnClick() method in the view, to recall other extensions specific to the DatePicker. Ideally, therefore we do not have inheritance, but this will be the subject of a second tutorial because it adds complexity.