473,394 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

asp:ListBox multiple selections

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
Nov 19 '05 #1
5 2886
You can consider using ArrayList instead of a standard array. ArrayList
allows you to dynamically add items to it.

"Chris Kettenbach" <c_**********@hotmail.com> wrote in message
news:YI********************@giganews.com...
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

Nov 19 '05 #2
yes, but it takes up more memory. Thank you very much though.

Chris

"Siva M" <sh******@online.excite.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
You can consider using ArrayList instead of a standard array. ArrayList
allows you to dynamically add items to it.

"Chris Kettenbach" <c_**********@hotmail.com> wrote in message
news:YI********************@giganews.com...
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

Nov 19 '05 #3
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" <sh******@online.excite.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
You can consider using ArrayList instead of a standard array. ArrayList
allows you to dynamically add items to it.

"Chris Kettenbach" <c_**********@hotmail.com> wrote in message
news:YI********************@giganews.com...
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

Nov 19 '05 #4
I agree. But, I thought OP used C#, not Visual Basic.

"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:ex**************@TK2MSFTNGP12.phx.gbl...
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" <sh******@online.excite.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
You can consider using ArrayList instead of a standard array. ArrayList
allows you to dynamically add items to it.

"Chris Kettenbach" <c_**********@hotmail.com> wrote in message
news:YI********************@giganews.com...
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


Nov 19 '05 #5
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" <sh******@online.excite.com> wrote in message
news:e0**************@TK2MSFTNGP15.phx.gbl...
I agree. But, I thought OP used C#, not Visual Basic.

"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:ex**************@TK2MSFTNGP12.phx.gbl...
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" <sh******@online.excite.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
You can consider using ArrayList instead of a standard array. ArrayList
allows you to dynamically add items to it.

"Chris Kettenbach" <c_**********@hotmail.com> wrote in message
news:YI********************@giganews.com...
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


Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Richard Fennell | last post by:
I am trying to use asp:listbox on a form, when a user selects a row in the asp:listbox a panel is displayed on the centre of the screen with a message, a bit like a model dialog on the form. The...
0
by: John Giblin | last post by:
I have a ListBox within a repeater tag and I am trying to set the selected value. I tried the following without success. I was also tryng to make a method for the selectedindex but I do not know...
1
by: Ray Valenti | last post by:
I have a ASP listbox that I am trying to populate with two fields, one for display (Category) and one to store (ID) as the selected item. I can successfully populate and view the list. How ever...
1
by: Daniel | last post by:
hi, I had an asp:listbox, and everytime i click item inside, the bar automatically go to the top, is there any way to keep the scroll position? I turn on the smartNavigation, it still doesn't...
2
by: Dan Nash | last post by:
Hi again! Right, okay... im now trying to get the value of an <asp:TextBox> control. so I have on my ASPX page... <asp:ListBox id="results" runat="server"></asp:ListBox> <asp:TextBox...
2
by: Simon Prince | last post by:
Help I have a ASP:Listbox on a form. My page Adds items to this this via Client-Side Script only. Such as... var vObj_TargetElement =...
3
by: Ryan Taylor | last post by:
Hello. I have an application where I need the user to be able to add items to a listbox. I've implemented this via javascript. The listbox is an <asp:ListBox>. However, when the user submits the...
2
by: Mark Rae | last post by:
Hi, Looking for some advice again... Imagine two ListBox controls denoting something like students and team membership e.g. many students can be members of many teams (e.g. the hockey team,...
7
by: mikeh3275 | last post by:
I'm creating a .net program that uploads images to the FTP server. A blank listbox is populated dynamically on the client side from the value of the html input file widget. There is also a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.