title: ASP.net listitem auto year range
body: <p>i have a registration form where a user fills in their date of birth.</p>
  
  <p>Is it possible to have a piece of code that will auto update the list item each time there is a new year? Saves me having to go back to each form and add a new year eg 2014</p>
  
  <pre><code>&lt;asp:DropDownList ID="date3" runat="server"&gt;
          &lt;asp:ListItem Value=""&gt;#&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;2013&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;2012&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;2011&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;2010&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;2009&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;2008&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;2007&lt;/asp:ListItem&gt;
  </code></pre>
  
  <p>I already use this snippet to auto add a year for the copyright statement on the master page <code>&lt;%=DateTime.Now.Year%&gt;</code></p>
  
  <p>ANSWER</p>
  
  <p>Code behind VB</p>
  
  <pre><code>Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
          If Not IsPostBack Then
              AutoYearRange()
  
          End If
      End Sub
  
      Public Sub AutoYearRange()
          date3.Items.Clear() '' This just ensures your list is now empty..
          For i As Integer = Now.AddYears(0).Year To Now.AddYears(-112).Year Step -1
              date3.Items.Add(New ListItem(i.ToString(), i.ToString()))
          Next
  
          date3.Items.Insert(0, New ListItem("- Please Select Date -", ""))
  
      End Sub
  </code></pre>
  
  <p>Thanks to Darren!</p>