locked
Dynamically bind dropdown with months names RRS feed

  • Question

  • User-1453200658 posted

    Hi there.

    I place a drop downlist into my web page, then add following code to bind items to dropdown list within the page load event.

    In this case is a drop downlist  with month names.

    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Globalization;
    using System;
    public partial class _Default : System.Web.UI.Page
    
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack )
            {
                DD_Monthbind();
            }
        }
    
        private void DD_Monthbind()
        {
            DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
            int currentMonth = DateTime.Now.Month;
    
            for (int i = 1; i < 13; i++)
            {
                bool isMonthInPast = i < currentMonth;
    
                if (!isMonthInPast)
                    DropDownList1.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));
            }
        }
    }

    On this drop downlist  when the month changes, the previous month or months are no longer displayed

    e.g.

    with date 01 February in the list disappears the month of January.

    I would need that the previous month is no longer shown after the 10th of the following month

    Can you help me?

    Wednesday, February 3, 2021 7:04 PM

Answers

  • User-1330468790 posted

    Hi Edward Sheriff Curtis,

     

    You could set the comparison in day-level.

    Regarding the code implementation, you could refer to below codes.

    aspx:

    <form id="form1" runat="server">
            <div>
                <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
            </div>
        </form>

    Code behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DD_Monthbind();
                }
            }
    
            private void DD_Monthbind()
            {
                // Use en-US as format information
                DateTimeFormatInfo info = CultureInfo.GetCultureInfo("en-US").DateTimeFormat;
    
                // get current date: for now, it is 2021/2/4
                DateTime now = DateTime.Now;
    
                // Comment above line and uncomment below line to do test: below date is 2021/2/11
                //DateTime now = new DateTime(DateTime.Now.Year,DateTime.Now.Month,11);
    
                // Set 10th day here
                int day = 10;
    
                for (int i = 1; i < 13; i++)
                {
                    // CompareTo method returns an integer: 
                    // Greater than zero => This instance is later than value. 
                    // Zero => This instance is the same as value.
                    // Less than zero => This instance is earlier than value.
                    int isBeforeTenthInPast = now.CompareTo(new DateTime(now.Year, i, day));
    
                    if (isBeforeTenthInPast <= 0)
                    {
                        DropDownList1.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));
                    }
                   
                }
    
            }

    Use 2021/2/4 to test (from my side it is now):

    Use 2021/2/11 to test:

     

    The method you should use is this one:  DateTime.CompareTo Method

      

    Hope helps.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 4, 2021 10:42 AM

All replies

  • User-1545767719 posted

    Why don't you add code to judge Day as you did for Month?

    By the way It seems that your question is about Web Forms. Here is MVC forum. Please use right forum.

    Wednesday, February 3, 2021 10:05 PM
  • User-1330468790 posted

    Hi Edward Sheriff Curtis,

     

    You could set the comparison in day-level.

    Regarding the code implementation, you could refer to below codes.

    aspx:

    <form id="form1" runat="server">
            <div>
                <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
            </div>
        </form>

    Code behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DD_Monthbind();
                }
            }
    
            private void DD_Monthbind()
            {
                // Use en-US as format information
                DateTimeFormatInfo info = CultureInfo.GetCultureInfo("en-US").DateTimeFormat;
    
                // get current date: for now, it is 2021/2/4
                DateTime now = DateTime.Now;
    
                // Comment above line and uncomment below line to do test: below date is 2021/2/11
                //DateTime now = new DateTime(DateTime.Now.Year,DateTime.Now.Month,11);
    
                // Set 10th day here
                int day = 10;
    
                for (int i = 1; i < 13; i++)
                {
                    // CompareTo method returns an integer: 
                    // Greater than zero => This instance is later than value. 
                    // Zero => This instance is the same as value.
                    // Less than zero => This instance is earlier than value.
                    int isBeforeTenthInPast = now.CompareTo(new DateTime(now.Year, i, day));
    
                    if (isBeforeTenthInPast <= 0)
                    {
                        DropDownList1.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));
                    }
                   
                }
    
            }

    Use 2021/2/4 to test (from my side it is now):

    Use 2021/2/11 to test:

     

    The method you should use is this one:  DateTime.CompareTo Method

      

    Hope helps.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 4, 2021 10:42 AM