1
votes

I have a listbox containing multiple items that is being populated on page load. Each item in the listbox can be selected without a problem except for the last item. When that one is clicked on the selection automatically jumps up to two items above. I can't seem to wrap my head around why this is happening. It only occurs when the last item is selected. The listbox is in an update panel within an ajax tab container, but I can't see why that would make a difference. I really hope I'm not missing something obvious...

Listbox:

 <asp:ListBox ID="availableServicesListBox" 
    runat="server" 
    class="formListBox" 
    AutoPostBack="True" 
    onselectedindexchanged="availableServicesListBox_SelectedIndexChanged"> 
 </asp:ListBox>

Page Load Event:

if (!Page.IsPostBack)         
{
      //populate from database    
}

selectedIndex changed event (for autopostback):

protected void availableServicesListBox_SelectedIndexChanged(object sender, EventArgs e)
{
   if (availableServicesListBox.SelectedValue.Length > 10)
   {
      servicePanel.Visible = true;
      activePanel.Visible = true;                
   }

   else
   {
      servicePanel.Visible = false;
   }
}
2
When this has happened to me, the last item had the same value as another item in the list. So when you select it it selects the first item that has that value, which occurs earlier in the list. Something to check. - Mike C.
if the issue is not what has been pointed by @MikeC. , you should give a closer at "populate from database" and see if the problems also occurs with hard-coded values - jbl
@MikeC. You're exactly right, the text properties are different but the values on certain items are the same because I was storing a query result there. Didn't even think about this - thank you! If you make your comment an answer I'll mark it. - ovaltein
Done and thanks. I like being exactly right! - Mike C.

2 Answers

1
votes

Check your list of values and make sure you do not have any duplicates. What I've seen in the past is you select a value later in the list, but it is a duplicate value for an item earlier in the list. So what happens is the selection is simply applied to the first matching item.

0
votes

If possible I suggest you do that using client side javascript/Jquery you can add on onChange event to your listBox and use jquery .Show(), .Hide() methods to display or hide your panels. It is also much faster. The only problem is that the panel is hidden from view, while using servicePanel.Visible = false; panel is not appearing on page at all.