Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Databound listbox web control 1

Status
Not open for further replies.

KDavie

Programmer
Feb 10, 2004
441
US
I am fairly new to ASP.NET so please forgive my ignorance.....

I have a listbox that I originally intended to bind to an underlying dataset. After binding the listbox to the dataset, and verifying it was being populated correctly, I noticed that the onIndexChanged event wasn't firing when the selection changed. I decided to try dynamically populating the listbox with a a SqlDataReader, doing this solved the problem.... Now the onIndexChanged event fires when expected. My question is, why couldn't I use a databound listbox to achieve the same functionality?.. Is this a shortcoming of the control or is there something obvious I am missing here?

The databinding was set up via the control's property box, therefore I am not posting any code with this thread.

Any feedback would be appreciated.

-Kevin

 
KDavie: Not exactly sure what might be going on here; seems the AutoPostBack property should trigger a postback, for example:
Code:
<asp:listbox id="lstA" runat="server" EnableViewState="true" OnSelectedIndexChanged = "lstA_SelectedIndexChanged"/>

So what you are saying is that you loose your PostBack if you bind to a DataSet but if you populate with a Reader you do not? Interesting problem -- should be solvable rather quickly .
 
So what you are saying is that you loose your PostBack if you bind to a DataSet but if you populate with a Reader you do not?

Not exactly; the listbox itself contains a list of filenames... EnableViewState is set in false. I store the value of the index whenever the selected index changes to a variable. I have a submit button that uses the variable to grab the associated URL from a dataset.

If the listbox is bound, the OnSelectedIndexChanged event doesn't fire and therefor the variable doesn't get a value.

If I use a Reader, everything works fine. This will serve as a permanent fix, so really everything is ok now.

Basically, since I am new to ASP.NET and I anticipate more ASP.NET projects in the future, I am trying to gain understanding of this behavior.

Interesting problem -- should be solvable rather quickly .

Any ideas?

Thanks for your help!

-Kevin
 
Ok Kevin - I see; I generally use a reader to populate the listbox and of course as you experienced have no problem recovering the selected value; binding the listbox to a DataView, etc. is something I have not experimented with.

One post I found used the following approach:
Code:
dim sItem as String
Try
 Dim drv as datarowview = Ctype(listbox1.selecteditem, datarowview)
 sItem = drv.item("MyColumn").tostring
Catch
....
end try
So the technique is quite different. Another poster approached the bound Listbox as follows:
Code:
If the listbox is data-bound, i.e. you have used DataSource property, the task is a bit more complex. If the listbox is bound to ArrayList cast every item to the array element type and check the display value. In case of a listbox bound to a DataTable or DataView each item can be cast to DataRowView which gives you an access to DataRow via Row property:

Sub SearchTextChanged(ByVal Sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
    Dim i As Int32, item As DataRowView
    For i = 0 To ListBox1.Items.Count - 1
        item = CType(ListBox1.Items(i), DataRowView)
        If CStr(item.Row(ListBox1.DisplayMember)).ToLower().StartsWith(TextBox1.Text.ToLower()) Then
            ListBox1.SelectedIndex = i
            Exit For
        End If
    Next i
End Sub

Do not expect this to be lightning fast. If you need really fast, consider caching display values in an array and search inside it. Having the items sorted by display value doesn't hurt either - you can then use BinarySearch methos which is way faster.
Like you KDavie I'll have to experiment with this a bit; I spend only part of my time maintaining ASP.NET pages and so this is a new approach with me as well. Try Goggling "Groups" using "DataList + bound" and see what you come up - several discussion are available as I reviewed a few looking up these arguments.
 
KDavie - one poster here had a similar problem that was eventually discovered to relate to the Page post back - see thread855-985564.
 
Thanks for your help Isadore... The last thread was the answer... It's kind of funny that it ended up being the PostBack property, as this is something I had to set to when I switched to the Reader to prevent the list from adding items on postbacks... I figured I must have been overlooking something simple.

-Kevin
 
Thanks KDavie - You have my curiosity up now about taking a closer look at the Datareader v. Binding for the Listbox - I have to date avoided the latter but like you I am also learning and I would like to know more about the advantageous and disadvantageous of both.
 
A DataReader is a forward only object. They are good to use when you need to just display data and not manipulate it, basically they are faster when compared to binding to a dataset. A dataset is basically an in memory database. They can have relations, be updated, inserted into, deleted from...etc.

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top