Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Issue with searching listview in c#

Posted on 2008-10-27
4
Medium Priority
?
1,667 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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

916 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