asp.net 2 list box and get selected items

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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!
 
Back
Top