Solved

Issue with searching listview in c#

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now