Add years to dropdown

Need a dropdown list with some years in it and a custom message as the first choice? How about this to start with: current year through 6 years into the future. Add a dropdownlist control to the page and call this function on pageload.

public void addYearsToDropdown(DropDownList whatDropdown, string ddmessage)
 {
      DropDownList list = whatDropdown as DropDownList;
      if(list != null)
      {
          list.Items.Insert(0, ddmessage);
      }
      int curYear = System.DateTime.Now.Year - 1;
      for(int i = 1; i < 6; i++)
      {
          list.Items.Insert(i, (curYear + i).ToString());
       }
 }

addMessageToDropdown(DropDownListYears, “–Select A Year–“);

Leave a Reply