asp.net 2 list box and get selected items

G

Guest

i made a list box and a button inside a form
that when the button pressed i have a function on the server that i s called :
<asp:ListBox id="ddl_lstGames" runat=server Rows=4 SelectionMode=Multiple>
</asp:ListBox><br/>
<asp:Button Text="Add Games!" runat=server OnClick="AddGames" />
and server side :
Protected Sub AddGames(Optional ByVal sender As Object = Nothing, Optional
ByVal e As EventArgs = Nothing)

End Sub

what i want to do : inside the AddGames sub, to get list of selected item
from thel ist box : there value and the value between the
<option>xxxx</option>

how do i do it?
thnaks in advance
peleg
 
N

Nathan Sokalski

I would use the GetSelectedIndices() method to get the indexes of the
selected items, and then iterate through the returned array using the
indexes with the Items property, something like the following:

Dim selectedindexes() As Integer = Me.ListBox1.GetSelectedIndices()

Dim curritem As ListItem

For Each index As Integer In selectedindexes

curritem = Me.ListBox1.Items(index)

Response.Write(curritem.Value)

Response.Write(curritem.Text)

Next


As long as you don't add any items client-side, this should work (I admit I
didn't try it out, but it seems simple enough). Good Luck!
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top