Solved

Issue with searching listview in c#

Posted on 2008-10-27
4
1,585 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where did System.Data.Objects go? 2 27
mail.To.Add 5 26
Help with adding data element is a specific row of an xml file using VB.NET 4 27
location of a form 2 14
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

773 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