Description:-
In this article I am going to explain how to populate dropdownlist with months name in asp.net using C# and vb.net.
System.Globalization namespace contains classes that define
culture-related information, including language, country/region, calendars in
use, and format patterns for dates, currency, and numbers, and sort order for
strings.
HTML:-
<fieldset style="width:50%"> <legend>Populate Dropdownlist with Months Names</legend> Select Country: <asp:DropDownList ID="ddlmonth" runat="server" AutoPostBack="true"> <asp:ListItem Value="0">--Select--</asp:ListItem> </asp:DropDownList> </fieldset>
First of all import the namespace
using System.Globalization;
Get month’s name:-
Create method to get month’s name
and call the method page load.
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetMonthName(); } }
public void GetMonthName() { try { DateTimeFormatInfo dtinfo = DateTimeFormatInfo.GetInstance(null); for (int i = 1; i <= 12; i++) { string monthname = dtinfo.GetMonthName(i); ddlmonth.Items.Add(new ListItem(monthname, i.ToString())); } } catch (Exception ex) { } }
protected void ddlmonth_SelectedIndexChanged(object sender, EventArgs e) { Response.Write("<script>alert('Selected Month is : "+ ddlmonth.SelectedItem +"')</script>"); }
Thanks for comments.....