anybody has some dropdowns datepicker for asp.net mvc
anybody knows where can I find a html helper or something that is going to开发者_如何学C generate a datepicker composed from 3 dropdowns ?
This is my little helper.
Itself explanatory, I believe. Can be tweaked to arrange the order of the drop-downs (Month/Day/Year or Day/Month/Year), and if you're using .NET 4, you can put default parameters for the names.
Edit: Cleared up the text to reduce eye bleeding
/// <summary>
/// Creates a days, months, years drop down list using an HTML select control.
/// The parameters represent the value of the "name" attribute on the select control.
/// </summary>
/// <param name="dayName">"Name" attribute of the day drop down list.</param>
/// <param name="monthName">"Name" attribute of the month drop down list.</param>
/// <param name="yearName">"Name" attribute of the year drop down list.</param>
/// <returns></returns>
public static string DatePickerDropDowns(this HtmlHelper html, string dayName, string monthName, string yearName)
{
TagBuilder daysList = new TagBuilder("select");
TagBuilder monthsList = new TagBuilder("select");
TagBuilder yearsList = new TagBuilder("select");
daysList.Attributes.Add("name", dayName);
monthsList.Attributes.Add("name", monthName);
yearsList.Attributes.Add("name", yearName);
StringBuilder days = new StringBuilder();
StringBuilder months = new StringBuilder();
StringBuilder years = new StringBuilder();
int beginYear = DateTime.UtcNow.Year - 100;
int endYear = DateTime.UtcNow.Year;
for (int i = 1; i <= 31; i++)
days.AppendFormat("<option value='{0}'>{0}</option>", i);
for (int i = 1; i <= 12; i++)
months.AppendFormat("<option value='{0}'>{0}</option>", i);
for (int i = beginYear; i <= endYear; i++)
years.AppendFormat("<option value='{0}'>{0}</option>", i);
daysList.InnerHtml = days.ToString();
monthsList.InnerHtml = months.ToString();
yearsList.InnerHtml = years.ToString();
return string.Concat(daysList.ToString(), monthsList.ToString(), yearsList.ToString());
}
Telerik has a library of some ASP.Net MVC controls that is free.
They work as helper methods and look pretty good. For example, the DatePicker works like this:
<%= Html.Telerik().DatePicker()
.Name("DatePicker")
.MinDate(Model.MinDate.Value)
.MaxDate(Model.MaxDate.Value)
.Value(Model.SelectedDate.Value)
.ShowButton(Model.ShowButton.Value)
%>
精彩评论