#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
DropDownListMonth Names
A fine is a tax for doing wrong. A tax is a fine for doing well.
__________________________________________________
Please remember to click “Mark as Answer” on the post that helps you.
A fine is a tax for doing wrong. A tax is a fine for doing well.
__________________________________________________
Please remember to click “Mark as Answer” on the post that helps you.
I changed it to work for all ListControls and converted it to VB.NET
Public Sub GetMonthList(ByVal MyddlMonthList As ListControl, ByVal SetCurruntMonth As Boolean)
Dim month As DateTime = DateTime.MinValue
For i As Integer = 0 To 11
Dim NextMont As DateTime = month.AddMonths(i)
Dim list As New ListItem()
list.Text = NextMont.ToString("MMMM")
list.Value = NextMont.Month.ToString()
MyddlMonthList.Items.Add(list)
Next
If SetCurruntMonth = True Then
MyddlMonthList.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = True
End If
and heres another version thanks to Eduardo Calixto
public void GetMyMonthList(DropDownList MyddlMonthList,bool SetCurruntMonth, string strCulture)
{
System.Globalization.DateTimeFormatInfo dti;
if ( String.IsNullOrEmpty( strCulture) )
{
// Get cultureinfo culture used by the resource manager
dti = System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat;
}
else
{
// Create a cultureinfo using the especified culture parameter
dti = new System.Globalization.CultureInfo(strCulture, false).DateTimeFormat;
}
for (int i = 1; i < 13; i++)
{
ListItem list = new ListItem(dti.GetMonthName(i), i.ToString() );
MyddlMonthList.Items.Add(list);
}
if (SetCurruntMonth == true)
{
MyddlMonthList.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
}
}
A fine is a tax for doing wrong. A tax is a fine for doing well.
__________________________________________________
Please remember to click “Mark as Answer” on the post that helps you.
etariq
Contributor
2823 Points
514 Posts
how to Creat a DropDownList with Month Names
Aug 01, 2008 04:55 PM|LINK
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> /// 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; } } #endregionDropDownList Month Names
__________________________________________________
Please remember to click “Mark as Answer” on the post that helps you.
photoshoper
Participant
947 Points
283 Posts
Re: how to Creat a DropDownList with Month Names
Aug 01, 2008 05:15 PM|LINK
Your code works perfectly fine for me. Not sure what your problem is...
etariq
Contributor
2823 Points
514 Posts
Re: how to Creat a DropDownList with Month Names
Aug 01, 2008 06:14 PM|LINK
[:$]nothing wrong with i just want to share it
__________________________________________________
Please remember to click “Mark as Answer” on the post that helps you.
ameenkpn
Contributor
4805 Points
814 Posts
Re: how to Creat a DropDownList with Month Names
Aug 04, 2008 10:05 AM|LINK
I just reduced the number of line, but still effective.
for (int i = 1; i <= 12; i++){
DateTime date = new DateTime(1900, i, 1);
ddlMonthName.Items.Add(new ListItem(date.ToString("MMMM"),i.ToString()));
}
ddlMonthName.SelectedValue =
DateTime.Today.Month.ToString();docluv
Star
12685 Points
2005 Posts
ASPInsiders
MVP
Re: how to Creat a DropDownList with Month Names
Aug 08, 2008 01:46 AM|LINK
I changed it to work for all ListControls and converted it to VB.NET
Public Sub GetMonthList(ByVal MyddlMonthList As ListControl, ByVal SetCurruntMonth As Boolean)
Dim month As DateTime = DateTime.MinValue
For i As Integer = 0 To 11
Dim NextMont As DateTime = month.AddMonths(i)
Dim list As New ListItem()
list.Text = NextMont.ToString("MMMM")
list.Value = NextMont.Month.ToString()
MyddlMonthList.Items.Add(list)
Next
If SetCurruntMonth = True Then
MyddlMonthList.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = True
End If
End Sub
etariq
Contributor
2823 Points
514 Posts
Re: how to Creat a DropDownList with Month Names
Aug 08, 2008 08:34 AM|LINK
and heres another version thanks to Eduardo Calixto
public void GetMyMonthList(DropDownList MyddlMonthList,bool SetCurruntMonth, string strCulture) { System.Globalization.DateTimeFormatInfo dti; if ( String.IsNullOrEmpty( strCulture) ) { // Get cultureinfo culture used by the resource manager dti = System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat; } else { // Create a cultureinfo using the especified culture parameter dti = new System.Globalization.CultureInfo(strCulture, false).DateTimeFormat; } for (int i = 1; i < 13; i++) { ListItem list = new ListItem(dti.GetMonthName(i), i.ToString() ); MyddlMonthList.Items.Add(list); } if (SetCurruntMonth == true) { MyddlMonthList.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true; } }__________________________________________________
Please remember to click “Mark as Answer” on the post that helps you.