Hi,
How to dynamically bind DropDownList with month names.
I place a drop DropDownList into my web page, then add following code to bind items to dropdown list within the page load event.
At this time in the DropDownList of months I have the months included from February to December
After the twentieth day of month February I will have in DropDownList the months included from March to December
The question is to see only March in DropDownList month and not see following months (from April to December...)
How can I also exclude from the list the months following the one on the list?
public partial class DD_Monthbind : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Monthbind();
}
}
private void Monthbind()
{
DateTimeFormatInfo info = DateTimeFormatInfo.GetInstance(null);
int currentMonth = DateTime.Now.Month;
for (int i = 1; i < 13; i++)
{
bool isMonthInPast = ((i + 1) < currentMonth) || (i + 1 == currentMonth && DateTime.Now.Day > 20);
if (!isMonthInPast)
DropDownList1.Items.Add(new ListItem(info.GetMonthName(i), i.ToString()));
}
}
}