3

anybody knows where can I find a html helper or something that is going to generate a datepicker composed from 3 dropdowns ?

Omu
  • 69,856
  • 92
  • 277
  • 407

2 Answers2

2

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());
}
Omar
  • 39,496
  • 45
  • 145
  • 213
  • I guess it requires some javascript for client side constraints, cuz now everybody will be able to select 31 february – Omu Mar 25 '10 at 09:19
  • Yeah, just be sure to put the constraint check server side also. – Omar Mar 25 '10 at 09:35
-1

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)
%>
Mark Ewer
  • 1,835
  • 13
  • 25