CattleRustler Posted December 22, 2003 Posted December 22, 2003 Hi everyone. I am using a webform listbox control on an aspx page. The list is populated with the file names from a particular directory. The weirdness starts in trying get the text of a selected item, I either get an error, a blank string, or a -1 of the SelectedIndex property, no matter which item is selected. This is very strange and doesn't happen in a winforms app, the list acts as expected. Are there any weird settings I am missing when using a webforms listbox? Thanks in advance James Quote mod2software Home of the VB.NET Class Builder Utility - Demo and Full versions available now!
*Gurus* Derek Stone Posted December 22, 2003 *Gurus* Posted December 22, 2003 The SelectedIndex property returns the index of the item, not the item text. Quote Posting Guidelines
CattleRustler Posted December 22, 2003 Author Posted December 22, 2003 no, I know that, I said I was getting errors so I used the SelectedIndex to see what it's value was and no matter what is selected the index returns -1 ! Quote mod2software Home of the VB.NET Class Builder Utility - Demo and Full versions available now!
kahlua001 Posted December 22, 2003 Posted December 22, 2003 Is ViewState enabled and do you check for IsPostBack? Quote
CattleRustler Posted December 22, 2003 Author Posted December 22, 2003 EnableViewState is True IsPostBack ? I am not to keen on this (newbie to asp.net) AutoPostBack is false ?? Quote mod2software Home of the VB.NET Class Builder Utility - Demo and Full versions available now!
CattleRustler Posted December 22, 2003 Author Posted December 22, 2003 The problem goes away if I manually populate the list with items from the Items Property Collection builder in the property sheet. maybe how I am populating the list in the first place is the problem? Here is the code... Private Sub PopulateList(ByVal sPath As String) Dim dir As Directory Dim fileArr() As String fileArr = dir.GetFiles(sPath) Dim intLoop As Integer lstFiles.Items.Clear() For intLoop = 0 To fileArr.GetUpperBound(0) lstFiles.Items.Add(CStr(fileArr.GetValue(intLoop))) Next intLoop End Sub Quote mod2software Home of the VB.NET Class Builder Utility - Demo and Full versions available now!
CattleRustler Posted December 22, 2003 Author Posted December 22, 2003 Page Load is empty but the following code is used to populate the listbox, when I use an intermediary collection to hold the file names, then pull them out into the list, the list then behaves as expected... Private Sub PopulateList(ByVal sPath As String) Dim dir As Directory Dim fileArr() As String fileArr = dir.GetFiles(sPath) Dim intLoop As Integer Dim intEnd As Integer = fileArr.GetUpperBound(0) Dim colFileNames As New Collection lstFiles.Items.Clear() For intLoop = 0 To intEnd colFileNames.Add(CStr(fileArr.GetValue(intLoop))) Next intLoop Dim strLooper As String For Each strLooper In colFileNames lstFiles.Items.Add(strLooper) Next colFileNames = Nothing End Sub strange ? Quote mod2software Home of the VB.NET Class Builder Utility - Demo and Full versions available now!
kahlua001 Posted December 22, 2003 Posted December 22, 2003 I think the problem has to do with how you are making the selection. If you are posting back to the page, you might be loosing the selectedItem on your postback. Quote
evaleah Posted December 29, 2003 Posted December 29, 2003 The postback issue mentioned above is almost surely your problem. I have this with datagrids quite often. I use a hidden html control to hold the value I want to keep and actually use that value when I execute code. I populate the html control with the OnSelectedItemChange event. HTH Eva Quote
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.