Question

i have a registration form where a user fills in their date of birth.

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

<asp:DropDownList ID="date3" runat="server">
        <asp:ListItem Value="">#</asp:ListItem>
        <asp:ListItem>2013</asp:ListItem>
        <asp:ListItem>2012</asp:ListItem>
        <asp:ListItem>2011</asp:ListItem>
        <asp:ListItem>2010</asp:ListItem>
        <asp:ListItem>2009</asp:ListItem>
        <asp:ListItem>2008</asp:ListItem>
        <asp:ListItem>2007</asp:ListItem>

I already use this snippet to auto add a year for the copyright statement on the master page <%=DateTime.Now.Year%>

ANSWER

Code behind VB

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

Thanks to Darren!

Was it helpful?

Solution

I would populate this programatically.

So, in your code behind, do something like this - this is VB

    date3.Items.Clear() '' This just ensures your list is now empty..
    For i As Integer = Now.AddYears(-5).Year To Now.AddYears(10).Year
        date3.Items.Add(New ListItem(i.ToString(), i.ToString()))
    Next

And in C#

   date3.Items.Clear(); // This just ensures your list is now empty..
   for (int i = Now.AddYears(-5).Year; i <= Now.AddYears(10).Year; i++) {
      date3.Items.Add(new ListItem(i.ToString(), i.ToString()));
   }

This will take todays year, subtract 5 from it for your starting point and then go up to 10 years in the future. You can change the -5 and 10 to suit whatever you want.

If you wanted to add something like a blank one at the start, say something like "Select Date" you could add this in after running the loop above.

Do something like this (VB):

    date3.Items.Insert(0, New ListItem("- Please Select Date -", ""))

Or C#

    date3.Items.Insert(0, new ListItem("- Please Select Date -", ""));

Update

To go in reverse you would simply start at the high number and go backwards, like this: (C#) - notice that we use i-- instead of i++ - this will subtract 1 on every loop rather than add one.

   date3.Items.Clear(); // This just ensures your list is now empty..
   for (int i = Now.AddYears(10).Year; i >= Now.AddYears(-5).Year; i--) {
      date3.Items.Add(new ListItem(i.ToString(), i.ToString()));
   }

And in VB

    For i As Integer = Now.AddYears(10).Year To Now.AddYears(-5).Year Step -1
        date3.Items.Add(New ListItem(i.ToString(), i.ToString()))
    Next
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top