wsyeager Posted October 15, 2003 Posted October 15, 2003 I have a listbox whose autopostback property is set to 'true'. When this event fires, I have code in the 'SelectedIndexChanged' event to capture the selectedindex of the listbox to be reset on the listbox when the postback occurs. However, the selectedindex property is alway -1. Below is the code prior to selecting an item for this listbox. Right after I select an item, the following code executes, which I'm expecting, but the "lstRider.SelectedIndex" value is -1, and I don't know why....... <code> Private Sub lstRider_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstRider.SelectedIndexChanged viewstate.Item("Rider1Idx") = lstRider.Items.Item(lstRider.SelectedIndex).Value() End Sub </code> Can anybody inform me how I can capture the index of the selected item in the list??? Quote Thanks, Bill Yeager (MCP, BCIP) Microsoft Certified Professional Brainbench Certified Internet Professional, .Net Programmer, Computer Programmer YeagerTech Consulting, Inc.
Moderators Robby Posted October 15, 2003 Moderators Posted October 15, 2003 Is the ViewState enabled? Quote Visit...Bassic Software
wsyeager Posted October 15, 2003 Author Posted October 15, 2003 (edited) selectedindex not being set yes, the viewsate is enabled....... If I use the following code, the value of the listbox is set properly when I'm first coming into the page (NOT POSTBACK): lstRider.SelectedIndex = lstRider.Items.IndexOf(lstRider.Items.FindByValue(CType(VIEWSTATE.Item("lngRider1UserID"), Long))) I need this value to set the item of the listbox in case the user decides to change the selected item in the listbox when the next postback occurs. viewstate.Item("Rider1Idx") = lstRider.Items.Item(lstRider.SelectedIndex).Value() However, the selectedindex is alway -1. Here is the code for my pageload event: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load DsRider1 = Cache.Get("Rider") 'ds to listbox is populated lstRider.DataBind() 'values appear in listbox If Not Page.IsPostBack Then lstRider.SelectedIndex = lstRider.Items.IndexOf(lstRider.Items.FindByValue(CType(VIEWSTATE.Item("lngRider1UserID"), Long))) else lstRider.SelectedIndex = lstRider.Items.IndexOf(lstRider.Items.FindByValue(CType(viewstate.Item("Rider1Idx"), Long)) 'value set in previous code snippet end if How am I supposed to capture the selected value of the item in the listbox????? Edited October 15, 2003 by Volte Quote Thanks, Bill Yeager (MCP, BCIP) Microsoft Certified Professional Brainbench Certified Internet Professional, .Net Programmer, Computer Programmer YeagerTech Consulting, Inc.
Administrators PlausiblyDamp Posted October 15, 2003 Administrators Posted October 15, 2003 you will probably find that DsRider1 = Cache.Get("Rider") 'ds to listbox is populated lstRider.DataBind() 'values appear in listbox is re-binding the listbox and losing the selected item - you may want to move the .DataBind bit into your PostBack chaeck. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
wsyeager Posted October 15, 2003 Author Posted October 15, 2003 Thanks, it was my mistake. I was expecting the selectedindexchanged event to fire for the listbox BEFORE the page got posted back. It was scrwing my logic up. I have the following code now and everything works fine: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load DsRider1 = Cache.Get("Rider") If Not Page.IsPostBack Then lstRider.DataBind() lstRider.SelectedIndex = lstRider.Items.IndexOf(lstRider.Items.FindByValue(CType(VIEWSTATE.Item("lngRider1UserID"), Long))) If lstRider.SelectedIndex > -1 Then lstRider2.Enabled = True End If lstRider2.SelectedIndex = lstRider.Items.IndexOf(lstRider.Items.FindByValue(CType(VIEWSTATE.Item("lngRider2UserID"), Long))) End if If lstRider.SelectedIndex > -1 Then lstRider2.Enabled = True viewstate.Item("Rider1IdxUserID") = lstRider.Items.Item(lstRider.SelectedIndex).Value() End If If lstRider2.SelectedIndex > -1 Then viewstate.Item("Rider2IdxUserID") = lstRider2.Items.Item(lstRider2.SelectedIndex).Value() End If End Sub Quote Thanks, Bill Yeager (MCP, BCIP) Microsoft Certified Professional Brainbench Certified Internet Professional, .Net Programmer, Computer Programmer YeagerTech Consulting, Inc.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.