Solved

Issue with searching listview in c#

Posted on 2008-10-27
4
1,606 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:nuvium
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:AmarIs26
ID: 22814378
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.

0
 

Author Comment

by:nuvium
ID: 22814396
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.
0
 

Author Comment

by:nuvium
ID: 22814806
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?
0
 
LVL 6

Accepted Solution

by:
AmarIs26 earned 500 total points
ID: 22817769
here try this, if you need explanation let me know
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestSite._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        .selected
        {
            background: blue;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
        <asp:Button ID="btnSearch" runat="server" Text="Button" OnClick="btnSearch_Click" />
        <asp:ListView ID="ListView1" runat="server">
            <LayoutTemplate>
                <table>
                    <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr runat="server" id="row">
                    <td>
                        <asp:Label ID="lblText" runat="server" Text='<%#Eval("text") %>'></asp:Label>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:ListView>
    </div>
    </form>
</body>
</html>
 
------------------------------------
 
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;
using System.Globalization;
 
namespace TestSite
{
    public class TestData
    {
        string text;
        public string Text
        {
            get { return text; }
            set { text = value; }
        }
 
        public TestData(string text)
        {
            this.text = text;
        }
    }
 
    public partial class _Default : System.Web.UI.Page
    {
 
        TestData[] data = new TestData[5];
 
        protected void Page_Load(object sender, EventArgs e)
        {
 
            if (!Page.IsPostBack)
            {
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = new TestData("data" + i.ToString());
                }
 
                ListView1.DataSource = data;
                ListView1.DataBind();
            }
        }
 
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in ListView1.Items)
            {
                Label lblText = item.FindControl("lblText") as Label;
                if (lblText != null && string.Compare(lblText.Text, txtSearch.Text, true, CultureInfo.InvariantCulture) == 0)
                {
                    // item found
                    HtmlTableRow row = item.FindControl("row") as HtmlTableRow;
                    if (row != null)
                    {
                        row.Attributes.Add("class", "selected");
                    }
                }
            }
        }
    }
}

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question