Hi
How to dynamically bind dropdown with months name.
I place a DropDownList into my web page, then add following code to bind items to dropdown list within the page load event.
In this case is a DropDownList with month names.
On this DropDownList when the month changes, the previous month or months are no longer displayed
e.g. with date 01 February in the list disappears the month of January.
I would need that the previous month is no longer shown after the 10th of the following month
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
DD_Monthbind();
}
}
private void DD_Monthbind()
{
DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
int currentMonth = DateTime.Now.Month;
for (int i = 1; i < 13; i++)
{
bool isMonthInPast = i < currentMonth;
if (!isMonthInPast)
DropDownList1.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));
}
}
}
Can you help me?