问题
Update I have solved the issue, however will finish posting this (solved prior to posting) in hopes it might help someone else out.
Issue: Referencing and manipulating listitems from a listbox requires the listbox's listitems to all have unique listbox.value. In my case, I was assigning the tablename as the listbox value and the columname as the listbox.text(I was giving the user a list of fields from a sql table to choose from). So a listbox containing the following:
Index 0: Text:street Value:"dbo.incident"
Index 1: Text:city Value:"dbo.incident"
Index 2: Text:state Value:"dbo.incident" Selected
And you then reference textbox1.selected item via code, it's handling is unexpected. It starts off knowing the selected item, but at some point in it's handling of the referenced listitem, it starts referencing it via value, at which point the return text or index would be 'street' or '0'. I understand once you start pulling items from a listbox, it pulls its first match.
begin original post
Okay project is a vb .net web forms project I have 2 listboxes that I want to populate and move entries back and forth, up and down. I understand how to do this, however i cannot seem to overcome this bizzare errant behavior that is happening.
If I add items manually like this it works fine, the listitems transfer between the listboxes and I can change the indexes as expected. (for this post, I will refer to entries created this was as static)
ListBox1.Items.Add(New ListItem("1", "1"))
...through...
ListBox1.Items.Add(New ListItem("10", "10"))
However if I add them programaticly from a linq statement, manual selection in the web page holds no bearing on selected item, it is always 0, clicking any button on the page, postback returns the listbox to index 0. (for this discussion, I will refer to entries created this way as dynamic)
Dim db As New MSCRMDataContext
Dim datamodel = db.Mapping
For Each r In datamodel.GetTables
If r.TableName = "dbo.IncidentFull" Then
For Each r1 In r.RowType.DataMembers
ListBox1.Items.Add(New ListItem(r1.MappedName, r.TableName))
Next
End If
Next
I have an aspx button:
<asp:Button ID="Button1" runat="server" Text="Button" />
That has the following click event:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mlbs_transferitem(ListBox1, ListBox2, ListBox1.SelectedItem)
End Sub
I have the sub that transfers the listitem in a separate .vb file
Public Class multilistboxselector
Shared Sub mlbs_transferitem(srclb As ListBox, dstlb As ListBox, lbitem As ListItem, Optional sort As Boolean = False)
'checks if srclb had a selected listitem
If IsNothing(lbitem) Then
Exit Sub
End If
'check for dupes
For Each li As ListItem In dstlb.Items
If li.Text = lbitem.Text Then
If li.Value = lbitem.Value Then
Exit Sub
End If
End If
Next
'add lbitem to dst
dstlb.SelectedIndex = -1
dstlb.Items.Add(lbitem)
'remove lbitem from src
srclb.Items.Remove(lbitem)
'sort dst
If sort = True Then
End If
End Sub
Just to be clear, the code works flawlessly with the static entries. I have created a new page and recreated everything there and the I have the same experience as my source page.
Edit
Trimming out my troubleshooting steps as they prove irrelevant to the issue and were quite lengthy.
来源:https://stackoverflow.com/questions/11547277/listbox-selects-wrong-item