Selected value from a listbox doesn't work

G

Guest

To all,

I have a listbox within a gridview that exists in an update panel. When I
try to get the values of each selected item in my listbox I get nothing.
When I'm in debug mode I get false for each item that I selected from my
listbox. I also get length=0 as a value for each item that I selected when I
use getselectedindices().

I have validated that I'm on the right row that has my listbox and I put my
gridview out of the update panel to see if there is a client side issue.
However, I'm still not able to get the selected values from the selected
items/index chosen from my list box. Can anyone help?

Here is my VB code behind:

Protected Sub btnSelectComputer_Click(ByVal sender As Object, ByVal e As
System.EventArgs)


Dim ChosenItems As String = Nothing

For Each gvrow As GridViewRow In gvADComputer.Rows

Dim lb As ListBox = CType(gvrow.FindControl("lbItems"), ListBox)

'Dim selections() As Integer = lb.GetSelectedIndices()

'Dim lb As ListBox = CType(FindControlRecursive(gvrow,
"lbItems"), ListBox)

For Each selectedItem As ListItem In lb.Items


If selectedItem.Selected Then 'Len(selectedItem.Value) > 0
Then

ChosenItems += selectedItem.Value + ", "

End If

Next

Next

tbTest1.Text = ChosenItems

End Sub
 
G

Guest

Manish,

Your last reponse to my last post about populating a listbox was very
helpful. Thank you so much for your help!

However, I now can't get the selected items out of the listbox. I have
followed your articles below and still can't get my selected items out of the
listbox. Can you make further recommendations?

Thanks,
Michael
 
G

Guest

Hi,
I have tried below code and it is working perfectly well:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ChosenItems As String = Nothing
Dim TotalChosenItems As String = Nothing

For Each gvrow As GridViewRow In GridView1.Rows
If CType(gvrow.FindControl("List1"), ListBox).SelectedIndex >= 0
Then
ChosenItems = CType(gvrow.FindControl("List1"),
ListBox).SelectedItem.Value
TotalChosenItems += ChosenItems
End If
Next
tbTest1.Text = TotalChosenItems

End Sub
Public Function GetDataSet() As System.Data.DataSet

Dim ds As New System.Data.DataSet()
Dim dt As New System.Data.DataTable()
Dim Row1, Row2, Row3 As System.Data.DataRow

Dim col1 As New System.Data.DataColumn()
col1.DataType = GetType(String)
col1.ColumnName = "Name"
dt.Columns.Add(col1)

Row1 = dt.NewRow()
Row1("Name") = "Manish Bafna"
dt.Rows.Add(Row1)

Row2 = dt.NewRow()
Row2("Name") = "Sanjay Bafna"
dt.Rows.Add(row2)

Row3 = dt.NewRow()
Row3("Name") = "Rakesh Bafna"
dt.Rows.Add(Row3)

ds.Tables.Add(dt)
Return ds

End Function

And in aspx page i have written following code:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ListBox ID="List1" DataTextField="Name"
DataValueField = "Name" DataSource= '<%# GetDataSet() %>' runat="server">
</asp:ListBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
If now it has answered your question then please do mark "Yes" below for
benefit of other users.Please feel free to ask if you are still have query
Have a nice day
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.
 
G

Guest

Thanks Manish!

I tried the same code on my co-workers machine and everything work as
planned. I think there is something wrong with my environment build on my
machine. I'm planning on re-imaging my machine or determine which assemblies
are not in synch with my co-workers.

Thanks so much for your time and efforts,
Michael
 

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

Top