Afternoon,
I am trying to apply persistence to a DatePicker so that when a user navigates to another page and then returns, the DatePicker retains its value. Here is my DatePicker:
@(Html.Kendo().DatePicker()
.Name("datepicker")
.Events(e =>
{
e.Change("onChangeDate");
})
.Format("dd/MM/yyyy")
.Max(DateTime.Today)
)
The DatePicker defaults to today’s date on the first entry to the page and stores that value in the session storage. This date is displayed successfully in the DatePicker.
$(document).ready(function () {
// Set the datePicker value
var date = setDate();
var datePicker = $("#datepicker").data("kendoDatePicker");
alert(date);
datePicker.value(date);
});
The alert confirms the date variable value: Tue Dec 14 2021 00:00:00 GMT+0000 (Greenwich Mean Time)
Here are my other functions:
function setDate() {
// Get the datePicker value from session storage of the browser.
var datePickerValue = sessionStorage.getItem("datePickerValue");
if (nullCheck(datePickerValue)) {
datePickerValue = kendo.date.today();
}
return datePickerValue;
}
function nullCheck(data) {
// Return true if null
return (typeof data !== 'undefined' && data !== null) ? false : true;
}
On return to the page, the alert confirms that the date variable value is still the same as before, but that date is not displayed in the DatePicker by datePicker.value(date); The DatePicker remains blank.
The onChangeDate function sets the session storage to the newly selected date.
I've tried setting the format of the data variable to dd/MM/yyyy, but that doesn't seem to make any difference. Plus it works on first entry to the page so it must be able to interpret the date value.
Kind regards,
Richard