Populating a Dropdown List on Page Load

Last post 09-27-2006 6:06 PM by PeterBrunone. 7 replies.

Sort Posts:

  • Populating a Dropdown List on Page Load

    09-27-2006, 12:05 AM

    I'd like to populate a dropdown list on page load, on a web form.

    Like this:

    <!-- make this automatically inset this year, and next year -->
    <%
    Dim thisYear
    Dim nextYear
    Date.Today.Year.ToString(thisYear)
    nextYear = thisYear + 1
    %>

    <asp:DropDownList ID="DateYear" runat="server">
    <asp:ListItem><% response.write(thisYear) %></asp:ListItem>
    <asp:ListItem><% response.write(nextYear) %></asp:ListItem>
    </asp:DropDownList>

    Obviously, that code does not work. I'd also like to use the code-behind way of doing things. I had a crack at using the Page_Init method, but didn't seem to be able to get anything happening.

    Any help much appreciated =).

  • Re: Populating a Dropdown List on Page Load

    09-27-2006, 1:20 AM
    Answer
    • Loading...
    • mokeefe
    • Joined on 08-20-2006, 5:15 AM
    • Canberra Australia
    • Posts 2,096

    Try this for an example

     

     

     

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="DropDownDate.aspx.vb" Inherits="DropDownDate" %>

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

     

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

        <title>Untitled Page</title>

    </head>

    <body>

        <form id="form1" runat="server">

        <div>

            <asp:DropDownList ID="DropDownList1" runat="server" Width="272px">

            </asp:DropDownList></div>

        </form>

    </body>

    </html>

     

     

    Code Behind

     

     

    Partial Class DropDownDate

        Inherits System.Web.UI.Page

     

        Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

     

            If Not Me.IsPostBack Then

     

                Dim Dates As New Collections.Generic.List(Of System.DateTime)

     

                ' Add next 5 years

                For i As Integer = 1 To 5

                    Dates.Add(System.DateTime.Now.AddYears(i))

                Next

     

                Me.DropDownList1.DataSource = Dates

                Me.DropDownList1.DataTextField = "Year"

     

                Me.DropDownList1.DataBind()

            End If

     

     

        End Sub

    End Class

     

     

    Regards,

    Martin.

     

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: Populating a Dropdown List on Page Load

    09-27-2006, 1:58 AM
    Thanks Martin, that worked out of the box =). I changed- 'Add next 5 years For i As Integer = 1 To 5 Dates.Add(System.DateTime.Now.AddYears(i)) Next -to- Dates.Add(System.DateTime.Now) 'Add next 5 years For i As Integer = 1 To 5 Dates.Add(System.DateTime.Now.AddYears(i)) Next - so that I got the current year as well. Cheers!
  • Re: Populating a Dropdown List on Page Load

    09-27-2006, 2:00 AM

    'Scuse formatting! That was- 

    'Add next 5 years
    For i As Integer = 1 To 5
      Dates.Add(System.DateTime.Now.AddYears(i))
    Next

    -to-

    Dates.Add(System.DateTime.Now)
    'Add next 5 years
    For i As Integer = 1 To 5
       Dates.Add(System.DateTime.Now.AddYears(i))
    Next

    - so that I got the current year as well.

  • Re: Populating a Dropdown List on Page Load

    09-27-2006, 2:43 AM
    • Loading...
    • mokeefe
    • Joined on 08-20-2006, 5:15 AM
    • Canberra Australia
    • Posts 2,096

    I actually dropped some other code that I had added that was superfluos to your question. I decided it was better to keep it to the point and I also dropped a similar line from the code regarding current year; expected you to do -

    ' Add this year and next 5 years

    For i As Integer = 0 To 5

    Very glad to be of help.

    Martin.

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: Populating a Dropdown List on Page Load

    09-27-2006, 12:16 PM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,663
    • TrustedFriends-MVPs

        This is just a nitpick really, but if you're going to create the data in a loop, you don't need a separate List object to hold the items.  Just put them into the list as you generate them, e.g.

    Partial Class DropDownDate

        Inherits System.Web.UI.Page

     

        Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

     

            If Not Me.IsPostBack Then

     

                ' Add next 5 years

                For i As Integer = 1 To 5

                    Me.DropDownList1.Items.Add(System.DateTime.Now.AddYears(i))

                Next


            End If

     

        End Sub

    End Class

     

    (minus the change you made later in the thread)
     

    Cheers,

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
  • Re: Populating a Dropdown List on Page Load

    09-27-2006, 5:48 PM
    • Loading...
    • mokeefe
    • Joined on 08-20-2006, 5:15 AM
    • Canberra Australia
    • Posts 2,096

    Hi Peter,

    Just another two bits worth. 

    I did consider the simpler approach, but felt it appropriate to provide a fuller example where the user could get a selected date as a DateTime object for later use.

    Martin.

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: Populating a Dropdown List on Page Load

    09-27-2006, 6:06 PM
    • Loading...
    • PeterBrunone
    • Joined on 06-19-2002, 9:15 AM
    • I'm standing behind you.
    • Posts 3,663
    • TrustedFriends-MVPs

    Point taken.

    Well played, sir.

    Peter Brunone
    MS MVP, ASP.NET
    Founder, EasyListBox.com
    Do the impossible, and go home early.
Page 1 of 1 (8 items)
Microsoft Communities
Page view counter