Home > ASP.Net > Dynamic DropDownList with Month Names

Dynamic DropDownList with Month Names


Hi

try this example to fill DropdownList with Month Names

1) create new website and add web page

2) In Default.aspx add dropdownlist control

3) In Code behind add the following code :-

    #region Method to Fill the a DropDownList with Month Names and set the Current Month Selected
    /// <summary>
    /// fills a dropDownlist with month list.
    /// </summary>
    /// The DropDown List that will Hold the Months.
    /// if set to <c>true</c> the Current Month will be selected.
    public void GetMyMonthList(DropDownList MyddlMonthList, bool SetCurruntMonth)
    {
        DateTime month = Convert.ToDateTime("1/1/2000");
        for (int i = 0; i < 12; i++)
        {
            DateTime NextMont = month.AddMonths(i);
            ListItem list = new ListItem();
            list.Text = NextMont.ToString("MMMM");
            list.Value = NextMont.Month.ToString();
            MyddlMonthList.Items.Add(list);
        }
        if (SetCurruntMonth == true)
        {
            MyddlMonthList.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
        }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetMyMonthList(DropDownList1, false);
        }
    }

Hope this helps

Good Luck.

Advertisements
Categories: ASP.Net
  1. Mohit
    October 4, 2011 at 3:14 pm

    I’m sorry my comment was for your first post in ClearAllControls() but posted here by mistake.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: