Chiru
Chiru

Reputation: 15

Display only the previous month's name in dropdown list

If current month is march it should only display only jan & feb, like wise if the current month is december it should list (jan to nov) dynamically.

And the default value should be January.

Upvotes: 1

Views: 182

Answers (1)

Mohsin Mehmood
Mohsin Mehmood

Reputation: 4236

You can do something like this:

var previousMonth = DateTime.Now.Month == 1 ? 1 : DateTime.Now.Month - 1
var months = Enumerable.Range(1, previousMonth).Select(i => new { I = i, M = DateTimeFormatInfo.CurrentInfo.GetMonthName(i) });


// <asp:DropDownList runat="server" ID="ddlMonths" />
ddlMonths.DataSource = months;
ddlMonths.DataTextField = "M";
ddlMonths.DataValueField = "I";
ddlMonths.DataBind();

Upvotes: 1

Related Questions