Fill Month Names and values to Dropdownlist dynamically in Asp.net


Add the following Namespace on the top for DateTimeFormatInfo:

using System.Globalization;


then write the following code in PageLoad:

protected void Page_Load(object sender, EventArgs e)
  {
               
   if (!Page.IsPostBack)
  {

        DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
        for (int i = 0; i < 12; i++)
        {
            ddlMonth.Items.Add(new ListItem(info.MonthNames[i], i.ToString()));
           
        }
        ListItem lsitem = new ListItem("Select Month", "0");
        ddlMonth.Items.Insert(0, lsitem);
    }

  }


0 comments:

Post a Comment