what's a neater way of writing this simple code...

Discussion in 'ASP .Net' started by Stimp, Oct 14, 2005.

  1. Stimp

    Stimp Guest

    I'm populating a dropdown list with non-consecutive values (well the
    last 3 values are non-consecutive anyway).. What's a shorter way of
    writing the following?...

    ddLetMaxPrice.Items.Insert(0, New ListItem("No Preference", 999999))
    ddLetMaxPrice.Items.Insert(1, New ListItem("100", 100))
    ddLetMaxPrice.Items.Insert(2, New ListItem("200", 200))
    ddLetMaxPrice.Items.Insert(3, New ListItem("300", 300))
    ddLetMaxPrice.Items.Insert(4, New ListItem("400", 400))
    ddLetMaxPrice.Items.Insert(5, New ListItem("500", 500))
    ddLetMaxPrice.Items.Insert(6, New ListItem("600", 600))
    ddLetMaxPrice.Items.Insert(7, New ListItem("700", 700))
    ddLetMaxPrice.Items.Insert(8, New ListItem("800", 800))
    ddLetMaxPrice.Items.Insert(9, New ListItem("900", 900))
    ddLetMaxPrice.Items.Insert(10, New ListItem("1200", 1200))
    ddLetMaxPrice.Items.Insert(11, New ListItem("1400", 1400))
    ddLetMaxPrice.Items.Insert(12, New ListItem("1600", 1600))
    ddLetMaxPrice.Items.Insert(13, New ListItem("1800", 1800))
    ddLetMaxPrice.Items.Insert(14, New ListItem("2000+", 999999))

    All I can seem to find on the net is to populate an ArrayList with the
    values and then use the arraylist as a datasource, but that also
    involves a line of code per item.

    I'm looking for something like:

    var aMaxPriceName = new Array("Not Selected", "2,000", "2,500", "3,000",
    "3,500", "4,000", "4,500", "5,000+");
    var aMaxPriceValue = new Array(999999999999, 2000, 2500,
    3000, 3500, 4000, 4500, 999999999999);

    and then to bind that array to the dropdownlist.
    Any ideas?
    --

    "I hear ma train a comin'
    .... hear freedom comin"
     
    Stimp, Oct 14, 2005
    #1
    1. Advertising

  2. Stimp

    Guest

    You could try something like this:

    Dim List() As String = {100, 200, 300, 400, 500, 600, 700}
    DropDownList3.DataSource = List
    DropDownList3.DataBind()
    DropDownList3.Items.Insert(0, New ListItem("No preference",
    999999))
    DropDownList3.Items.Insert(DropDownList3.Items.Count, New
    ListItem("2000+", 999999))

    Hope that helps,
    Scott
     
    , Oct 14, 2005
    #2
    1. Advertising

  3. Stimp

    parez Guest

    vb.net? or c#
    Stimp wrote:
    > I'm populating a dropdown list with non-consecutive values (well the
    > last 3 values are non-consecutive anyway).. What's a shorter way of
    > writing the following?...
    >
    > ddLetMaxPrice.Items.Insert(0, New ListItem("No Preference", 999999))
    > ddLetMaxPrice.Items.Insert(1, New ListItem("100", 100))
    > ddLetMaxPrice.Items.Insert(2, New ListItem("200", 200))
    > ddLetMaxPrice.Items.Insert(3, New ListItem("300", 300))
    > ddLetMaxPrice.Items.Insert(4, New ListItem("400", 400))
    > ddLetMaxPrice.Items.Insert(5, New ListItem("500", 500))
    > ddLetMaxPrice.Items.Insert(6, New ListItem("600", 600))
    > ddLetMaxPrice.Items.Insert(7, New ListItem("700", 700))
    > ddLetMaxPrice.Items.Insert(8, New ListItem("800", 800))
    > ddLetMaxPrice.Items.Insert(9, New ListItem("900", 900))
    > ddLetMaxPrice.Items.Insert(10, New ListItem("1200", 1200))
    > ddLetMaxPrice.Items.Insert(11, New ListItem("1400", 1400))
    > ddLetMaxPrice.Items.Insert(12, New ListItem("1600", 1600))
    > ddLetMaxPrice.Items.Insert(13, New ListItem("1800", 1800))
    > ddLetMaxPrice.Items.Insert(14, New ListItem("2000+", 999999))
    >
    > All I can seem to find on the net is to populate an ArrayList with the
    > values and then use the arraylist as a datasource, but that also
    > involves a line of code per item.
    >
    > I'm looking for something like:
    >
    > var aMaxPriceName = new Array("Not Selected", "2,000", "2,500", "3,000",
    > "3,500", "4,000", "4,500", "5,000+");
    > var aMaxPriceValue = new Array(999999999999, 2000, 2500,
    > 3000, 3500, 4000, 4500, 999999999999);
    >
    > and then to bind that array to the dropdownlist.
    > Any ideas?
    > --
    >
    > "I hear ma train a comin'
    > ... hear freedom comin"
     
    parez, Oct 14, 2005
    #3
  4. Stimp

    Stimp Guest

    Either.. I'm working in VB, but can translate from C# :)

    On Fri, 14 Oct 2005 parez <> wrote:
    > vb.net? or c#
    > Stimp wrote:
    >> I'm populating a dropdown list with non-consecutive values (well the
    >> last 3 values are non-consecutive anyway).. What's a shorter way of
    >> writing the following?...
    >>
    >> ddLetMaxPrice.Items.Insert(0, New ListItem("No Preference", 999999))
    >> ddLetMaxPrice.Items.Insert(1, New ListItem("100", 100))
    >> ddLetMaxPrice.Items.Insert(2, New ListItem("200", 200))
    >> ddLetMaxPrice.Items.Insert(3, New ListItem("300", 300))
    >> ddLetMaxPrice.Items.Insert(4, New ListItem("400", 400))
    >> ddLetMaxPrice.Items.Insert(5, New ListItem("500", 500))
    >> ddLetMaxPrice.Items.Insert(6, New ListItem("600", 600))
    >> ddLetMaxPrice.Items.Insert(7, New ListItem("700", 700))
    >> ddLetMaxPrice.Items.Insert(8, New ListItem("800", 800))
    >> ddLetMaxPrice.Items.Insert(9, New ListItem("900", 900))
    >> ddLetMaxPrice.Items.Insert(10, New ListItem("1200", 1200))
    >> ddLetMaxPrice.Items.Insert(11, New ListItem("1400", 1400))
    >> ddLetMaxPrice.Items.Insert(12, New ListItem("1600", 1600))
    >> ddLetMaxPrice.Items.Insert(13, New ListItem("1800", 1800))
    >> ddLetMaxPrice.Items.Insert(14, New ListItem("2000+", 999999))
    >>
    >> All I can seem to find on the net is to populate an ArrayList with the
    >> values and then use the arraylist as a datasource, but that also
    >> involves a line of code per item.
    >>
    >> I'm looking for something like:
    >>
    >> var aMaxPriceName = new Array("Not Selected", "2,000", "2,500", "3,000",
    >> "3,500", "4,000", "4,500", "5,000+");
    >> var aMaxPriceValue = new Array(999999999999, 2000, 2500,
    >> 3000, 3500, 4000, 4500, 999999999999);
    >>
    >> and then to bind that array to the dropdownlist.
    >> Any ideas?
    >> --
    >>
    >> "I hear ma train a comin'
    >> ... hear freedom comin"

    >



    --

    "I hear ma train a comin'
    .... hear freedom comin"
     
    Stimp, Oct 14, 2005
    #4
  5. Stimp,

    I think you could bind an array vs. the array list that would be easier to
    populate.

    Dim MyArray as String() = { "100", "200", "300" }

    lstPizzaTopping.DataSource = MyArray
    lstPizzaTopping.DataBind()


    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Stimp" <> wrote in message
    news:...
    > I'm populating a dropdown list with non-consecutive values (well the
    > last 3 values are non-consecutive anyway).. What's a shorter way of
    > writing the following?...
    >
    > ddLetMaxPrice.Items.Insert(0, New ListItem("No Preference", 999999))
    > ddLetMaxPrice.Items.Insert(1, New ListItem("100", 100))
    > ddLetMaxPrice.Items.Insert(2, New ListItem("200", 200))
    > ddLetMaxPrice.Items.Insert(3, New ListItem("300", 300))
    > ddLetMaxPrice.Items.Insert(4, New ListItem("400", 400))
    > ddLetMaxPrice.Items.Insert(5, New ListItem("500", 500))
    > ddLetMaxPrice.Items.Insert(6, New ListItem("600", 600))
    > ddLetMaxPrice.Items.Insert(7, New ListItem("700", 700))
    > ddLetMaxPrice.Items.Insert(8, New ListItem("800", 800))
    > ddLetMaxPrice.Items.Insert(9, New ListItem("900", 900))
    > ddLetMaxPrice.Items.Insert(10, New ListItem("1200", 1200))
    > ddLetMaxPrice.Items.Insert(11, New ListItem("1400", 1400))
    > ddLetMaxPrice.Items.Insert(12, New ListItem("1600", 1600))
    > ddLetMaxPrice.Items.Insert(13, New ListItem("1800", 1800))
    > ddLetMaxPrice.Items.Insert(14, New ListItem("2000+", 999999))
    >
    > All I can seem to find on the net is to populate an ArrayList with the
    > values and then use the arraylist as a datasource, but that also
    > involves a line of code per item.
    >
    > I'm looking for something like:
    >
    > var aMaxPriceName = new Array("Not Selected", "2,000", "2,500", "3,000",
    > "3,500", "4,000", "4,500", "5,000+");
    > var aMaxPriceValue = new Array(999999999999, 2000, 2500,
    > 3000, 3500, 4000, 4500, 999999999999);
    >
    > and then to bind that array to the dropdownlist.
    > Any ideas?
    > --
    >
    > "I hear ma train a comin'
    > ... hear freedom comin"
     
    S. Justin Gengo, Oct 14, 2005
    #5
  6. Stimp

    Stimp Guest

    On Fri, 14 Oct 2005 <> wrote:
    > You could try something like this:
    >
    > Dim List() As String = {100, 200, 300, 400, 500, 600, 700}
    > DropDownList3.DataSource = List
    > DropDownList3.DataBind()
    > DropDownList3.Items.Insert(0, New ListItem("No preference",
    > 999999))
    > DropDownList3.Items.Insert(DropDownList3.Items.Count, New
    > ListItem("2000+", 999999))


    that's probably the best I can do.. thanks!


    --

    "I hear ma train a comin'
    .... hear freedom comin"
     
    Stimp, Oct 14, 2005
    #6
    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. John Mott

    writing a simple code generator

    John Mott, Feb 21, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    386
    John Mott
    Feb 21, 2006
  2. Ross Clement (Email address invalid - do not use)

    Neater method of creating a linked list

    Ross Clement (Email address invalid - do not use), Nov 30, 2005, in forum: C Programming
    Replies:
    2
    Views:
    286
    Richard Harnden
    Nov 30, 2005
  3. lovecreatesbeauty

    Is writing C code very simple?

    lovecreatesbeauty, Dec 28, 2005, in forum: C Programming
    Replies:
    16
    Views:
    624
    Giannis Papadopoulos
    Dec 30, 2005
  4. Bill Cunningham

    neater code

    Bill Cunningham, Jan 31, 2010, in forum: C Programming
    Replies:
    14
    Views:
    657
    Ian Collins
    Mar 1, 2010
  5. Replies:
    9
    Views:
    114
    Brad Baxter
    May 1, 2006
Loading...

Share This Page