vb.net Listbox (multiple selection) not working on initialization

jkerwin used Ask the Experts™
I have a listbox control that is in multiple selection mode.

<asp:listbox id="listMultiChapter" name="listMultiChapter" runat="server" selectionmode="Multiple" width="250px"></asp:listbox>

Open in new window

I am looping through this listbox to grab all selections and then store them.

For Each li As ListItem In listMultiChapter.Items
                If li.Selected Then
                    If strMultiChapter = "" Then
                        strMultiChapter = li.Text
                        strMultiChapter = strMultiChapter & "|" & li.Text
                    End If
                End If

Open in new window

This code had been working fine until recently.  At SOME point, this code no longer works but only when I initially save a record.  I use this SAME CODE when updating a record and it works fine.  My testing has determined the following:

1)  The code is showing NO selections - I have tested to make sure no li.selected come back as true.
2)  The code after is working fine - I set the strMultiChapter after this code snippit and it gets inserted into the record correctly.
3) I fill the listbox slightly differently when it comes back to an edit (I pass in an id which removes certain items from the box depending on selection).  However, I have manually removed this from the update routine and it still saves on edit and does not on insert.

Has anyone had any experience with .net controls not initializing correctly?  I do use the AJAX toolkit.  I recently 'skinned' the HTMLEditor on this form but I also removed that skin as well to see if that was affecting this - still no solution.

Any input greatly appreciated!  Thanks!

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Muhammad ZamanSoftware Development Lead



AppendDataBoundItems="true" in your list box tag

hope it will help



AppendDataBoundItems to true did not fix.  Thanks for the attempt!

            myConnection = New SqlConnection(connstring)
            Dim cmd As New SqlDataAdapter(strCommand, myConnection)
            Dim ds As DataSet = New DataSet()
            cmd.Fill(ds, "ChapterData")
            listMultiChapter.DataTextField = "nvcChapterName"
            'listMultiChapter.DataValueField = "idChapter"
            listMultiChapter.DataSource = ds

Commenting out the datavaluefield fixed this!  I ended up not using the value (numeric), I just store the names separated by a delimiter.  However, I don't see why this impacted the 'selected' value?!

Thanks all for taking a look!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial