asp:ListBox multiple selections

Discussion in 'ASP .Net' started by Chris Kettenbach, Oct 7, 2005.

  1. Good morning, Does anyone happen to know if there's a way to make an array
    of the selected items in an asp:ListBox? I know you can loop through and
    check the selected property, my question is how do you know how large to
    make your array without looping through the list twice.

    I had like button_click(object sender, EventArgs e)

    {

    ListBox lb = (ListBox)Page.FindControl("myListBox");

    foreach(ListItem li in lb.items)

    { if (li.Selected)

    { //some code here to populate the array with the li.value }

    }



    Thanks for any advice.



    Chris
     
    Chris Kettenbach, Oct 7, 2005
    #1
    1. Advertising

  2. Chris Kettenbach

    Siva M Guest

    You can consider using ArrayList instead of a standard array. ArrayList
    allows you to dynamically add items to it.

    "Chris Kettenbach" <> wrote in message
    news:...
    Good morning, Does anyone happen to know if there's a way to make an array
    of the selected items in an asp:ListBox? I know you can loop through and
    check the selected property, my question is how do you know how large to
    make your array without looping through the list twice.

    I had like button_click(object sender, EventArgs e)

    {

    ListBox lb = (ListBox)Page.FindControl("myListBox");

    foreach(ListItem li in lb.items)

    { if (li.Selected)

    { //some code here to populate the array with the li.value }

    }



    Thanks for any advice.



    Chris
     
    Siva M, Oct 7, 2005
    #2
    1. Advertising

  3. yes, but it takes up more memory. Thank you very much though.

    Chris

    "Siva M" <> wrote in message
    news:%...
    > You can consider using ArrayList instead of a standard array. ArrayList
    > allows you to dynamically add items to it.
    >
    > "Chris Kettenbach" <> wrote in message
    > news:...
    > Good morning, Does anyone happen to know if there's a way to make an array
    > of the selected items in an asp:ListBox? I know you can loop through and
    > check the selected property, my question is how do you know how large to
    > make your array without looping through the list twice.
    >
    > I had like button_click(object sender, EventArgs e)
    >
    > {
    >
    > ListBox lb = (ListBox)Page.FindControl("myListBox");
    >
    > foreach(ListItem li in lb.items)
    >
    > { if (li.Selected)
    >
    > { //some code here to populate the array with the li.value }
    >
    > }
    >
    >
    >
    > Thanks for any advice.
    >
    >
    >
    > Chris
    >
    >
    >
     
    Chris Kettenbach, Oct 7, 2005
    #3
  4. Siva,

    You can always ReDim the array. One parameter of the ReDim allows you to
    ReDim while keeping the old values but there are issues when using ReDim
    Preserve as this article points out:

    http://www.aspheute.com/english/20001025.asp

    You may be better off in the long run to just use an ArrayList anyway.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Siva M" <> wrote in message
    news:%...
    > You can consider using ArrayList instead of a standard array. ArrayList
    > allows you to dynamically add items to it.
    >
    > "Chris Kettenbach" <> wrote in message
    > news:...
    > Good morning, Does anyone happen to know if there's a way to make an array
    > of the selected items in an asp:ListBox? I know you can loop through and
    > check the selected property, my question is how do you know how large to
    > make your array without looping through the list twice.
    >
    > I had like button_click(object sender, EventArgs e)
    >
    > {
    >
    > ListBox lb = (ListBox)Page.FindControl("myListBox");
    >
    > foreach(ListItem li in lb.items)
    >
    > { if (li.Selected)
    >
    > { //some code here to populate the array with the li.value }
    >
    > }
    >
    >
    >
    > Thanks for any advice.
    >
    >
    >
    > Chris
    >
    >
    >
     
    S. Justin Gengo, Oct 7, 2005
    #4
  5. Chris Kettenbach

    Siva M Guest

    I agree. But, I thought OP used C#, not Visual Basic.

    "S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
    message news:...
    Siva,

    You can always ReDim the array. One parameter of the ReDim allows you to
    ReDim while keeping the old values but there are issues when using ReDim
    Preserve as this article points out:

    http://www.aspheute.com/english/20001025.asp

    You may be better off in the long run to just use an ArrayList anyway.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Siva M" <> wrote in message
    news:%...
    > You can consider using ArrayList instead of a standard array. ArrayList
    > allows you to dynamically add items to it.
    >
    > "Chris Kettenbach" <> wrote in message
    > news:...
    > Good morning, Does anyone happen to know if there's a way to make an array
    > of the selected items in an asp:ListBox? I know you can loop through and
    > check the selected property, my question is how do you know how large to
    > make your array without looping through the list twice.
    >
    > I had like button_click(object sender, EventArgs e)
    >
    > {
    >
    > ListBox lb = (ListBox)Page.FindControl("myListBox");
    >
    > foreach(ListItem li in lb.items)
    >
    > { if (li.Selected)
    >
    > { //some code here to populate the array with the li.value }
    >
    > }
    >
    >
    >
    > Thanks for any advice.
    >
    >
    >
    > Chris
    >
    >
    >
     
    Siva M, Oct 7, 2005
    #5
  6. Siva,

    Maybe you missed this small bit of text in the article?

    ReDim allows increasing as well as decreasing an array's size. For this, a
    new array is created in each instance. The reason for this is that the
    VB.NET array is a descendant of the System.Array of the .NET Runtime which
    by definition has a fixed size on creation. In C# this is obvious, as is
    demonstrated by the following code emulating ReDim:

    string[] arrTest = new string[1];
    // and now we want to change the size: ReDim arrTest(20)
    arrTest = new string[20];
    In itself, this is no problem, the trouble starts with Preserve (today's
    topic). When using Redim with the Preserve keyword, the old elements are
    preserved - as copies in the new array.

    From here it wouldn't be that difficult to write a loop that would copy the
    original array into the new one...

    But why do that when ArrayList already exists was my point.


    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Siva M" <> wrote in message
    news:...
    >I agree. But, I thought OP used C#, not Visual Basic.
    >
    > "S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
    > message news:...
    > Siva,
    >
    > You can always ReDim the array. One parameter of the ReDim allows you to
    > ReDim while keeping the old values but there are issues when using ReDim
    > Preserve as this article points out:
    >
    > http://www.aspheute.com/english/20001025.asp
    >
    > You may be better off in the long run to just use an ArrayList anyway.
    >
    > --
    > Sincerely,
    >
    > S. Justin Gengo, MCP
    > Web Developer / Programmer
    >
    > www.aboutfortunate.com
    >
    > "Out of chaos comes order."
    > Nietzsche
    > "Siva M" <> wrote in message
    > news:%...
    >> You can consider using ArrayList instead of a standard array. ArrayList
    >> allows you to dynamically add items to it.
    >>
    >> "Chris Kettenbach" <> wrote in message
    >> news:...
    >> Good morning, Does anyone happen to know if there's a way to make an
    >> array
    >> of the selected items in an asp:ListBox? I know you can loop through and
    >> check the selected property, my question is how do you know how large to
    >> make your array without looping through the list twice.
    >>
    >> I had like button_click(object sender, EventArgs e)
    >>
    >> {
    >>
    >> ListBox lb = (ListBox)Page.FindControl("myListBox");
    >>
    >> foreach(ListItem li in lb.items)
    >>
    >> { if (li.Selected)
    >>
    >> { //some code here to populate the array with the li.value }
    >>
    >> }
    >>
    >>
    >>
    >> Thanks for any advice.
    >>
    >>
    >>
    >> Chris
    >>
    >>
    >>

    >
    >
    >
     
    S. Justin Gengo, Oct 7, 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. bill yeager

    multiple listbox selections

    bill yeager, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    461
    bill yeager
    Aug 12, 2003
  2. =?Utf-8?B?QWRpcw==?=
    Replies:
    0
    Views:
    393
    =?Utf-8?B?QWRpcw==?=
    Oct 19, 2004
  3. zdrakec
    Replies:
    0
    Views:
    537
    zdrakec
    Jul 22, 2005
  4. zdrakec
    Replies:
    0
    Views:
    478
    zdrakec
    Jul 22, 2005
  5. bthomasIDFG
    Replies:
    0
    Views:
    150
    bthomasIDFG
    Oct 12, 2005
Loading...

Share This Page