Solved

Issue with searching listview in c#

Posted on 2008-10-27
4
1,635 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 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