问题
I have a Telerik MVC DatePicker :
<%: Html.Telerik().DatePickerFor(model => model.Date)
.TodayButton()
.Max(DateTime.Now)
.HtmlAttributes(new { @onkeypress = "return allowNumbersWithDot(event);", @onpaste = "return false;" })
%>
By setting Max property, I get dates only till current date and all future dates are hidden. What I what is, enable all dates till current date and disable all future dates. i.e. user can see all dates but can select only till the current date.
回答1:
The Max property blocks the selection and appearance of all dates after the value set. To get around this, try formatting your DatePicker with a client event handler like:
@Html.Telerik().DatePickerFor(model => model.Date).ClientEvents(e => e.OnChange("onChange"))
and then add your validation with something like this:
function onChange(e) {
var endDate = new Date(); // Your specific date
if (e.value > endDate) {
if (e.previousValue === null) {
$(this).data("tDatePicker").value(null);
}
e.preventDefault();
}
}
More information about this technique can be found in the Telerik support forums at: http://www.telerik.com/community/forums/aspnet-mvc/datepicker/datepicker---prevent-future-date-selection.aspx
回答2:
Minimum Date: Use it to set the smallest date, which the user can select.
<%= Html.Telerik().DatePicker()
.Name("DatePicker")
.MinDate(new DateTime(1900,1,1)))
%>
Maximum Date: Use it to set the biggest date, which the user can select.
<%= Html.Telerik().DatePicker()
.Name("DatePicker")
.MaxDate(new DateTime(2100,1,1)))
%>
Good Luck!
来源:https://stackoverflow.com/questions/16216788/telerik-mvc-datepicker-disable-all-future-dates