views:

276

answers:

3

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

A: 

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
no, that's a datepicker with a calendar, I want one composed from 3 dropdowns
Omu
+1  A: 

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());
}
Baddie
I guess it requires some javascript for client side constraints, cuz now everybody will be able to select 31 february
Omu
Yeah, just be sure to put the constraint check server side also.
Baddie
A: 

How can this helper class be tied to a data column in a data annotations class? ..

femi