Link to home
Start Free TrialLog in
Avatar of nuvium
nuvium

asked on

Issue with searching listview in c#

I am trying to implement search functionality on a listview that is dynamically generated from a sql query. This is a listview on a web form using the 3.5 framework.

The listview itself is pretty striaghtforward. Here is the code I am firing on the search button press (the text comes from a search textbox called 'txtSearch'). I got this code from Experts-Exchange from a thread initiated from someone doing something similar. I get the following errors on my compile:

1. Error      13      'System.Web.UI.WebControls.ListView' does not contain a definition for 'View' and no extension method 'View' accepting a first argument of type 'System.Web.UI.WebControls.ListView' could be found (are you missing a using directive or an assembly reference?)

2. Error      14      'System.Web.UI.WebControls.View' does not contain a definition for 'List'      

3. Error      15      'System.Web.UI.WebControls.ListView' does not contain a definition for 'FindItemWithText' and no extension method 'FindItemWithText' accepting a first argument of type 'System.Web.UI.WebControls.ListView' could be found (are you missing a using directive or an assembly reference?)      

4. Error      16      'System.Web.UI.WebControls.ListView' does not contain a definition for 'TopItem' and no extension method 'TopItem' accepting a first argument of type 'System.Web.UI.WebControls.ListView' could be found (are you missing a using directive or an assembly reference?)

Can anyone help me? If the code that I am using on the search button isn't correct, can someone help me pick another method? Thanks!

Also, here are the classes I am importing into the codebehind, if it helps:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


protected void btnSearch_Click(object sender, EventArgs e)
        {
            ListViewItem foundItem =
            ListView1.FindItemWithText(txtSearch.Text, false, 0, true);
            if (foundItem != null)
            {
                ListView1.TopItem = foundItem;
 
            }
 
        }

Open in new window

Avatar of AmarIs26
AmarIs26

I think you have confused a sample from windows forms ListView with ASP.Net listview. There is no such method called FindItemWithText in asp.net ListView.

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.finditemwithtext.aspx

Your best bet is to loop through each item in the listview once it has been generated and compare it with the search criteria.

Avatar of nuvium

ASKER

Ahhhh, that would make sense considering the errors generated. I inherited this solution from another programmer, and its been shifted from 1.1 to 2.0 to 3.5 and I figured something might have gotten screwed up in the assemblies, but this makes more sense. Do you have any sample code? I'm kind of new to C# from VB, and I get screwed up with the syntax sometimes.
Avatar of nuvium

ASKER

I have the code that loops through the items in the listview down fine, just having trouble writing the code to match the row with the text in the textbox, and "select it". Not sure how that will work, the listview by default has ALOT of rows. I was thinking maybe it can be set as the top return? or somehow highlited and focused?
ASKER CERTIFIED SOLUTION
Avatar of AmarIs26
AmarIs26

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial