Monday 16 January 2012

How to populate month name in DropDownList in ASP.NET


In order to populate the month name in the DropDownList, you can use below code snippet.

DropDownList code
<asp:DropDownList ID="dropDown1" runat="server"></asp:DropDownList>


Namespace to use
using System.Globalization;


Code behind
DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);

        for (int i = 1; i < 13; i++)

        {

            //Response.Write(info.GetAbbreviatedMonthName(i) + "<br />");

            dropDown1.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));

        }


In the above code, commented code shows how to write abbreviated month name too, so in case you do not want to show the full month name in the dropdown, use GetAbbreviatedMonthName method.

No comments:

Post a Comment

Total Pageviews