Saturday, October 13, 2018

Bind Drop Down List With Month Name and Year without Database Using ASP.NET C#

Bind Drop Down List With Month Name and Year without Database Using ASP.NET C# 


HTML

<!--  Month Name -->
 <asp:DropDownList ID="ddlMonthName" runat="server" class="form-control">
  </asp:DropDownList>

<!--  Year List -->
 <asp:DropDownList ID="ddlYearsList" runat="server" class="form-control">
  </asp:DropDownList>


C# Code

using System.Globalization;


protected void Page_Load(object sender, EventArgs e)
    {
 if (!Page.IsPostBack)
        {
            //Month Name
            var months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
            for (int i = 0; i < months.Length; i++)
            {
                ddlMonthName.Items.Add(new ListItem(months[i], i.ToString()));
            }

            // Year List
            for (int i = DateTime.Now.Year; i >= 2014; i--)
            {
                ddlYearsList.Items.Add(i.ToString());

            }
            ddlYearsList.Text = DateTime.Now.ToString("yyyy");
        }
}

No comments:

Post a Comment