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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filtering a ListBox

Status
Not open for further replies.

stevenk140

Programmer
Nov 20, 2003
34
CA
I am working with ASP.NET (C#) and I have a listbox that I would like to filter by the user entering text into a textbox next to the list.

How could I accomplish this, and is there any code sample available?

Steve
 
steve: This example is in VB, but might help - sure there are several approaches -- in this case a user types in a string value (textbox) and then clicks on a button that triggers the following event (to populate the listbox):

Private Sub listContactsList()
listContacts.Items.Clear()
Dim strSQL = "'%" & txtSearch.Text & "%'"
Dim dbconnCt As OleDbConnection = New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=" & Server.MapPath(".\fpdb\Contacts.mdb;"))
Dim DBCommand = New OleDbCommand ("SELECT Contact_ID, ContactName, Last FROM WebMasterContacts WHERE Last Like" & strSQL, dbconnCt)
''" & txtSearch.Text & "'" & "ORDER BY Last ASC", dbconnCt)
Dim reader As OleDbDataReader
Try
dbconnCt.Open()
reader = DBCommand.ExecuteReader()
Do While reader.Read()
Dim NewItem As New ListItem()
NewItem.Value = reader("Contact_ID")
NewItem.Text = reader("ContactName")
listContacts.Items.Add(NewItem)
Loop
reader.Close()
Catch err As Exception
lblContacts.Text = "Error reading list of names."
lblContacts.Text &= err.Message
Finally
If (Not dbconnCt Is Nothing) Then
dbconnCt.Close()
End If
End Try
End Sub
 
Thanks for the sample, but is there a way to do this real time with out creating a new connection/reloading the page?
 
Steve - I don't have the details in front of me but I recall reading an article where you can save the dataset on the client side, and filter -- I'll see if I can dig it up (in most cases you would post back - but if the dataset is not too large, I believe there is a way to store it on the client side and use javascript from there).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top