how to Creat a DropDownList with Month Names
#region Method to Fill the a DropDownList with Month Names and set the Current Month 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
thats it Happy Coding
| You must Sign In to use this message board. |
|
| | Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh) | FirstPrevNext |
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
Hello ZeroDev,
I did some changes in your function. Now you can set any culture that you want or use the same culture that the resource manager (probably the last option is the better but i did it complete to ilustrate how you can change the culture)
public void GetMyMonthList(DropDownList MyddlMonthList,bool SetCurruntMonth, string strCulture) { System.Globalization.DateTimeFormatInfo dti;
if ( String.IsNullOrEmpty( strCulture) ) { dti = System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat; } else { 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; } }
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
 |
good this is cool to , if u want to keep a culture always on specific one no mater what and if u wann akeep it dynamic no need for the culture in this function coool thanks
Life is really simple, but we insist on making it complicated
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
General
News
Question
Answer
Joke
Rant
Admin