populating asp:dropdownlist programmatically

Discussion in 'ASP .Net' started by amessimon, Mar 6, 2004.

  1. amessimon

    amessimon Guest

    I need to display a drop down list which holds up to 250 listitems.

    I'd like to create this programmatically rather than have to hardcode it
    into the page.

    For example

    <asp:DropDownList id="numYears" runat="server" name="numYears">
    <asp:ListItem Value="0" Text="0 years" Selected="true" />
    <asp:ListItem Value="1" Text="1 year"/>
    <asp:ListItem Value="2" Text="2 year"/>
    <asp:ListItem Value="2" Text="2 year"/>
    <asp:ListItem Value="3" Text etc............. up to 250 years

    Thanks in advance..

    Simon Ames
    amessimon, Mar 6, 2004
    #1
    1. Advertising

  2. amessimon

    Teemu Keiski Guest

    Hi,

    put the DDL declaration at the Page (or create it in code just as you see
    better).

    <asp:DropDownList ID="ddl1" Runat="server">
    </asp:DropDownList>

    Then say in Page_Load:

    = = =
    if(!Page.IsPostBack)
    {
    for(int i=0;i<251;i++)
    {
    ListItem litem=new ListItem(i.ToString() + " years",i.ToString());
    ddl1.Items.Add(litem);
    }
    ddl1.SelectedIndex =0;
    }
    = = =

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist



    "amessimon" <> wrote in message
    news:...
    I need to display a drop down list which holds up to 250 listitems.

    I'd like to create this programmatically rather than have to hardcode it
    into the page.

    For example

    <asp:DropDownList id="numYears" runat="server" name="numYears">
    <asp:ListItem Value="0" Text="0 years" Selected="true" />
    <asp:ListItem Value="1" Text="1 year"/>
    <asp:ListItem Value="2" Text="2 year"/>
    <asp:ListItem Value="2" Text="2 year"/>
    <asp:ListItem Value="3" Text etc............. up to 250 years

    Thanks in advance..

    Simon Ames
    Teemu Keiski, Mar 6, 2004
    #2
    1. Advertising

  3. amessimon

    Simon Gorski Guest

    Hello Simon
    "amessimon" <> schrieb im Newsbeitrag
    news:...
    > I need to display a drop down list which holds up to 250 listitems.
    >
    > I'd like to create this programmatically rather than have to hardcode it
    > into the page.
    >
    > For example
    >
    > <asp:DropDownList id="numYears" runat="server" name="numYears">
    > <asp:ListItem Value="0" Text="0 years" Selected="true" />
    > <asp:ListItem Value="1" Text="1 year"/>
    > <asp:ListItem Value="2" Text="2 year"/>
    > <asp:ListItem Value="2" Text="2 year"/>
    > <asp:ListItem Value="3" Text etc............. up to 250 years
    >


    for (int i=0; i<=250; i++)

    {

    numYears.Items.Add(i.ToString());

    }

    mfg simon g.
    Simon Gorski, Mar 6, 2004
    #3
  4. amessimon

    niranjan44

    Joined:
    Jun 2, 2011
    Messages:
    4
    niranjan44, Jun 2, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hazzard
    Replies:
    3
    Views:
    5,038
    Hazzard
    Jul 14, 2003
  2. =?Utf-8?B?Q0hSSVM=?=

    Problem populating dropdownlist control

    =?Utf-8?B?Q0hSSVM=?=, Apr 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    331
    Cowboy \(Gregory A. Beamer\)
    Apr 13, 2004
  3. Wim
    Replies:
    1
    Views:
    428
  4. Joe Delphi

    Populating DropDownList Items

    Joe Delphi, Jun 30, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,035
    Karl Seguin
    Jun 30, 2005
  5. =?Utf-8?B?UkZTNjY2?=
    Replies:
    3
    Views:
    5,458
    Bruce Barker
    Nov 14, 2005
Loading...

Share This Page