Getting Started with the DateRangePicker
This tutorial explains how to set up a basic Telerik UI for ASP.NET Core DateRangePicker and highlights the major steps in the configuration of the component.
You will initialize a DateRangePicker control with a number of tools. Next, you will handle some of the DateRangePicker events. Finally, you can run the sample code in Telerik REPL and continue exploring the components.
Prerequisites
To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET Core components:
You can use the Telerik REPL playground and skip installing the components on your system and configuring a project.
-
You can prepare a Visual Studio project by following either of these guides:
Creating a new pre-configured project for the Telerik UI for ASP.NET Core components from a project template.
Manually configuring an existing project as described in the First Steps on Windows or First Steps on Mac articles.
1. Prepare the CSHTML File
The first step is to add the required directives at the top of the .cshtml
document:
-
To use the Telerik UI for ASP.NET Core HtmlHelpers:
@using Kendo.Mvc.UI
-
To use the Telerik UI for ASP.NET Core TagHelpers:
@addTagHelper *, Kendo.Mvc
Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and apply some basic styles.
2. Initialize the DateRangePicker
Use the DateRangePicker HtmlHelper or TagHelper to add the component to a page:
The
Name()
configuration method is mandatory as its value is used for theid
and thename
attributes of the DateRangePicker element.The
Culture()
configuration method specifies the culture info used by the widget.Date
andtime
values typically vary by culture. For example, the"d"
standard format string indicates that a date and time value is to be displayed using a short date pattern. For the invariant culture, this pattern is"MM/dd/yyyy"
. For thefr-FR
culture, it is"dd/MM/yyyy"
. For theja-JP
culture, it is"yyyy/MM/dd"
.The
Labels
configuration method determines if the labels for the inputs will be visible.
@using Kendo.Mvc.UI
@(Html.Kendo().DateRangePicker()
.Name("daterangepicker")
.Culture("fr-FR")
.Labels(true)
)
@addTagHelper *, Kendo.Mvc
<kendo-daterangepicker name="daterangepicker" culture="fr-FR" labels="true" >
</kendo-daterangepicker>
3. Handle the DateRangePicker Events
The DateRangePicker exposes various events that you can handle and further customize the functionality of the component. In this tutorial, you will use the Open
, Close
, and Change
events of the DateRangePicker.
@using Kendo.Mvc.UI
@(Html.Kendo().DateRangePicker()
.Name("daterangepicker")
.Culture("fr-FR")
.Labels(true)
.Events(e=> e.Open("onOpen").Close("onClose").Change("onChange"))
)
<script>
function onOpen() {
console.log("Open");
}
function onClose() {
console.log("Close");
}
function onChange() {
var range = this.range();
console.log("Change :: start - " + kendo.toString(range.start, 'd') + " end - " + kendo.toString(range.end, 'd'));
}
</script>
@addTagHelper *, Kendo.Mvc
<kendo-daterangepicker name="daterangepicker" culture="fr-FR" labels="true" style="width: 100%;"
on-open="onOpen"
on-close="onClose"
on-change="onChange">
</kendo-daterangepicker>
<script>
function onOpen() {
console.log("Open");
}
function onClose() {
console.log("Close");
}
function onChange() {
var range = this.range();
console.log("Change :: start - " + kendo.toString(range.start, 'd') + " end - " + kendo.toString(range.end, 'd'));
}
</script>
For more examples, refer to the demo on using the events of the DateRangePicker.
4. (Optional) Reference Existing DateRangePicker Instances
To use the client-side API of the DateRangePicker and build on top of its initial configuration, you need a reference to the DateRangePicker instance. Once you get a valid reference, you can call the respective API methods:
-
Use the
.Name()
(id
attribute) of the component instance to get a reference.<script> var dateRangePickerReference = $("#daterangepicker").data("kendoDateRangePicker"); // DateRangePicker Reference is a reference to the existing instance of the helper. </script>
-
Use the client-side API of the DateRangePicker to control the behavior of the widget. In this example, you will use the
enable
method to disable the DateRangePicker.<script> $(document).ready(function () { var daterangepicker = $("#daterangepicker").data("kendoDateRangePicker"); daterangepicker.enable(false); }) </script>
For more information on referencing specific helper instances, see the Methods and Events article.
Explore this Tutorial in REPL
You can continue experimenting with the code sample above by running it in the Telerik REPL server playground:
Next Steps
- Configuring the Selected Dates of the DateRangePicker
- Changing the Appearance of the DateRangePicker
- Using Validation with the DateRangePicker