asp:ListBox multiple selections

  • Thread starter Chris Kettenbach
  • Start date
C

Chris Kettenbach

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

Siva M

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

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

S. Justin Gengo

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
 
S

Siva M

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

message 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
 
S

S. Justin Gengo

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top