brettvienna
New Member
This seems like a simple task but has become quite frustrating...<BR><BR>I have a listbox which gets filled initially in the page_load within an "If not page.ispostback" and there are say, 4 items in it, no item is yet selected. <BR><BR>When the user clicks on an item, the ListBox.SelectedIndexChanged event-sub gets called (autopostback property is set to true). The very first time you select an item, it acknowledges the changed index and works fine (calls another sub that refills the listbox with the appropriate list depending on what you selected). <BR><BR>The problem is, that every other time after that, when the SelectedIndexChanged event-sub gets called the Selected index is always 0, regardless of what index you just selected! Is the postback wiping everything out before it's getting to the SelectedIndexChanged event? How can I get the selected item before it gets erased? <BR><BR>I would rather not set the autopostback property to false and force the user to use a button, but can't see any other way around it. I also tried it with a combobox and it behaves the same way, gets reset to the first item it's been databound to. What is having the autopostback set to true good for if its selected item gets reset before you can do anything with it?<BR><BR>Here's the event-sub, any ideas?<BR><BR>----------------------------------------<BR> Private Sub lstHeadings_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstHeadings.SelectedIndexChanged<BR><BR> Dim strWholePath As String<BR> lblHeadingName.Text = lstHeadings.SelectedItem.Text<BR> strWholePath = Server.MapPath("..HelpXML\" & lstHeadings.SelectedItem.Value)<BR> FillLists(lstHeadings.SelectedItem.Text, strWholePath)<BR><BR> End Sub<BR>----------------------------------------<BR><BR>Thanks!<BR>I.Hi,<BR><BR>I don't know what exactly goes wrong, but I am 4 sure it's not the listbox that causes the problem.<BR>I tested this with a very simple sample and it works fine.<BR>The viewstate keeps track of al data if a postback occurs.<BR>First try to keep it simple and then add your code and look when it goes wrong.<BR><BR>Simple test:<BR><BR> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<BR> If Not Page.IsPostBack Then<BR> ListBox1.Items.Add(New ListItem("test1", 1))<BR> ListBox1.Items.Add(New ListItem("test2", 2))<BR> ListBox1.Items.Add(New ListItem("test3", 3))<BR> ListBox1.Items.Add(New ListItem("test4", 4))<BR> End If<BR> End Sub<BR><BR> Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged<BR> Label1.Text = ListBox1.SelectedIndex.ToString<BR> FillList(ListBox1.SelectedItem.Text)<BR> End Sub<BR><BR> Private Sub FillList(ByVal mySelItem As String)<BR> ListBox2.Items.Add(New ListItem(ListBox1.SelectedItem.Text, ListBox1.SelectedItem.Value))<BR> End Sub<BR><BR>Bye and good luck.You're right, it wasn't the listbox's problem. Thanks for the suggestion to go back to the basics.<BR><BR>Turns out that in a ListBox or a DropDown the ".text" property can be whatever you want, and the ".value" property can also be whatever you want BUT it must be unique in the collection. <BR><BR>I'm used to binding the ".value" to an ID field from a database, so I never worried about having to have a unique value. But this time I'm grabbing items from an XML doc and was wanting to store one of the attributes as the value... and ofcourse I ran into this prob. because not all the attribute values are unique.<BR><BR>Looking back, it seems obvious that the ".value" property should be unique... but I think they should have called it ".ID" though to be more explicit. <BR><BR>What a pain.<BR>I.<BR>