locked
Drop down list for DateTime field to select months RRS feed

  • Question

  • User-1336852631 posted

    I'm using VS2010 with ASP.NET 4 and SQLServer 2008 and have created a dynamic data web site.

    One of my tables has a DateTime field in the database and this is presented as a text field in the web site.  I want to have a drop down list for the field to allow the user to select months:

    Jan-12
    Feb-12
    Mar-12
    ...
    Dec-13

    If the user selected Jan-12, the DateTime field would be populated with 01-Jan-2012.

    Ideally I'd want the two year date span covered by the months in the drop down list to roll forward when we hit 2013 to Jan-13 to Dec-14.

    I've looked at using the EnumDataTypeAttribute but this seems to only work with int32 and string fields.  I've also considered creating a custom field template but am unsure how to populate the drop down list that then populates the date time.

    Any advice would be gratefully received.

    Sunday, April 29, 2012 4:52 AM

Answers

  • User-330204900 posted

    Hi BigA, here you go:

    <%@ Control Language="C#" CodeBehind="DateList_Edit.ascx.cs" Inherits="DateListSample.DynamicData.FieldTemplates.DateList_EditField" %>
     
    <asp:DropDownList ID="DropDownList1" runat="server" CssClass="DDDropDown">
    </asp:DropDownList>
    

    public partial class DateList_EditField : System.Web.DynamicData.FieldTemplateUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Mode == DataBoundControlMode.Insert || !Column.IsRequired)
                    DropDownList1.Items.Add(new ListItem("[Not Set]"""));
    
                var thisYear = DateTime.Today.Year;
                var startDate = new DateTime(thisYear - 1, 1, 1);
                var endDate = new DateTime(thisYear, 12, 31);
                for (int y = thisYear - 1; y < thisYear + 1; y++)
                {
                    for (int m = 1; m < 13; m++)
                    {
                        var date = new DateTime(y, m, 1);
                        var monthName = date.ToString("MMM");
                        var shortYear = date.ToString("yy");
                        DropDownList1.Items.Add(new ListItem(String.Format("{0}-{1}", monthName, shortYear), date.ToShortDateString()));
                    }
                }
            }
        }
    
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
    
            var date = (DateTime)FieldValue;
            var selectedItem = DropDownList1.Items.FindByValue(date.ToShortDateString());
    
            if (selectedItem != null)
                DropDownList1.SelectedValue = selectedItem.Value;
        }
    
        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            dictionary[Column.Name] = ConvertEditedValue(DropDownList1.SelectedValue);
        }
    
        public override Control DataControl
        {
            get { return DropDownList1; }
        }
    

    here is the code benind and markup have fun.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 1, 2012 5:57 AM

All replies

  • User-330204900 posted

    Hi Biga, I think that would be an easy one for you do want two dropdowns first with month and the second with years? I can have a go at it for you tomorrow :)

    Sunday, April 29, 2012 6:08 PM
  • User-1336852631 posted

    Hi,  Thanks in advance for your help.  I'm just looking for one drop down list with month-year combined (e.g. Jan-11).

    Monday, April 30, 2012 3:47 AM
  • User-330204900 posted

    Hi BigA, here you go:

    <%@ Control Language="C#" CodeBehind="DateList_Edit.ascx.cs" Inherits="DateListSample.DynamicData.FieldTemplates.DateList_EditField" %>
     
    <asp:DropDownList ID="DropDownList1" runat="server" CssClass="DDDropDown">
    </asp:DropDownList>
    

    public partial class DateList_EditField : System.Web.DynamicData.FieldTemplateUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Mode == DataBoundControlMode.Insert || !Column.IsRequired)
                    DropDownList1.Items.Add(new ListItem("[Not Set]"""));
    
                var thisYear = DateTime.Today.Year;
                var startDate = new DateTime(thisYear - 1, 1, 1);
                var endDate = new DateTime(thisYear, 12, 31);
                for (int y = thisYear - 1; y < thisYear + 1; y++)
                {
                    for (int m = 1; m < 13; m++)
                    {
                        var date = new DateTime(y, m, 1);
                        var monthName = date.ToString("MMM");
                        var shortYear = date.ToString("yy");
                        DropDownList1.Items.Add(new ListItem(String.Format("{0}-{1}", monthName, shortYear), date.ToShortDateString()));
                    }
                }
            }
        }
    
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
    
            var date = (DateTime)FieldValue;
            var selectedItem = DropDownList1.Items.FindByValue(date.ToShortDateString());
    
            if (selectedItem != null)
                DropDownList1.SelectedValue = selectedItem.Value;
        }
    
        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            dictionary[Column.Name] = ConvertEditedValue(DropDownList1.SelectedValue);
        }
    
        public override Control DataControl
        {
            get { return DropDownList1; }
        }
    

    here is the code benind and markup have fun.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 1, 2012 5:57 AM
  • User-1336852631 posted

    Wow thanks Steve!  That really helps and its good to gain an understanding of how to customise things in the way you have.  

    I altered your code a little to meet my requirements (I needed the dates to span from Jan-12 to Dec-13 and I think your startDate and endDate variables weren't used).  Also on insert I got an exception so had to add a check for the FieldValue being null in OnDataBinding():

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (Mode == DataBoundControlMode.Insert || !Column.IsRequired)
                        DropDownList1.Items.Add(new ListItem("[Not Set]", ""));
    
                    var thisYear = DateTime.Today.Year;
                    for (int y = thisYear; y <= thisYear + 1; y++)
                    {
                        for (int m = 1; m < 13; m++)
                        {
                            var date = new DateTime(y, m, 1);
                            var monthName = date.ToString("MMM");
                            var shortYear = date.ToString("yy");
                            DropDownList1.Items.Add(new ListItem(String.Format("{0}-{1}", monthName, shortYear), date.ToShortDateString()));
                        }
                    }
                }
            }
    
            protected override void OnDataBinding(EventArgs e)
            {
                base.OnDataBinding(e);
                if (FieldValue != null) 
                {
                    var date = (DateTime)FieldValue;
                    var selectedItem = DropDownList1.Items.FindByValue(date.ToShortDateString());
    
                    if (selectedItem != null)
                        DropDownList1.SelectedValue = selectedItem.Value;
                }
            }



    Tuesday, May 1, 2012 10:10 AM
  • User-330204900 posted

    yes I was in a rush refactoring :)

    Tuesday, May 1, 2012 11:38 AM