Click here to Skip to main content
15,886,075 members
Articles / Web Development / HTML
Article

Dynamic DropDownList with Month Names

Rate me:
Please Sign up or sign in to vote.
2.98/5 (17 votes)
13 Jul 2007CPOL 101.8K   18   11
Method to Fill the a DropDownList with Month Names and set the Current Month Selected
how to Creat a DropDownList with Month Names
#region Method to Fill the a DropDownList with Month Names and set the Current Month Selected
        /// <summary>
        /// fills a dropDownlist with month list.
        /// </summary>
        /// <param name="MyddlMonthList" />The DropDown List that will Hold the Months.</param />
        /// <param name="SetCurruntMonth" />if set to <c>true</c> the Current Month will be selected.</param />
        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
thats it Happy Coding

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Jordan Jordan
Life is really simple, but we insist on making it complicated

Comments and Discussions

 
QuestionShort Code and simple Pin
The Zero31-Jan-15 8:43
The Zero31-Jan-15 8:43 
GeneralMy vote of 4 Pin
Diwakar Gupta16-Dec-11 1:53
Diwakar Gupta16-Dec-11 1:53 
GeneralGood Job Pin
Prakash Varun 201014-Jul-10 19:33
Prakash Varun 201014-Jul-10 19:33 
GeneralOther option Pin
christianfdiazm27-Nov-09 6:24
christianfdiazm27-Nov-09 6:24 
GeneralHere is a VB Version Pin
Mr. Hef2-Oct-07 5:35
Mr. Hef2-Oct-07 5:35 
Here is the code that I found works nicely to do almost the same thing in VB

Private Sub BindMonths()
'Step through each possible month value
For month As Integer = 1 To 12
'Add the Month Name and Month Value to the Drop Down List
ddlMonth.Items.Add(New ListItem(MonthName(month, False), month.ToString()))
Next
'Add A Select Month Option At Position Zero In The Month List
ddlMonth.Items.Insert(0, New ListItem("Select Month", "0"))
End Sub
GeneralRe: Here is a VB Version Pin
ZeroDev3-Oct-07 7:08
ZeroDev3-Oct-07 7:08 
GeneralSome changes in your code Pin
Eduardo Calixto13-Jul-07 3:24
Eduardo Calixto13-Jul-07 3:24 
GeneralRe: Some changes in your code Pin
ZeroDev13-Jul-07 3:30
ZeroDev13-Jul-07 3:30 
GeneralThanks Pin
ZeroDev13-Mar-07 21:37
ZeroDev13-Mar-07 21:37 
GeneralExactly what I wanted Pin
toxaq13-Mar-07 10:06
toxaq13-Mar-07 10:06 
GeneralGood Job!!! Pin
Domingo M. Asuncion6-Mar-07 15:31
Domingo M. Asuncion6-Mar-07 15:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.