使用C#按TextBox过滤ListView

时间:2016-11-02 17:02:13

标签: c# asp.net listview filter ektron

我有一个显示作业列表的ListView控件。我有一行LinkBut​​ton控件,点击时按工作的各个字段对列表进行排序。

我还在页面上有一个TextBox控件和LinkBut​​ton控件。

我想输入一个关键字来过滤TextBox中的ListView。单击LinkBut​​ton控件后,ListView过滤器将仅显示关键字包含在我们按日期排序的4种模式之一(日期,公司,标题,位置)的结果。

我还没有包含其他代码和类,我是C#的新手,所以如果我遗漏了需要的内容,请告诉我。

文本框:

<asp:TextBox ID="txtKeySearch" CssClass="form-control" runat="server" />

按钮:

<asp:LinkButton ID="lnkbtnKeySearch" runat="server" class="fancy" >
<span>Search <span class="ico-chevron-right"></span></span></asp:LinkButton>

代码背后:

using Ektron.Custom.SmartForms;
using Ektron.Custom.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;

public partial class Source_Controls_Alumni_AlumniJobListing : System.Web.UI.UserControl
{

    // Added Property
    private long _containerId = 0;
    public long ContainerID
    {
        get { return _containerId; }
        set { _containerId = value; }
    }
    /////////

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData(AJVMCompareMode.Date, AJVMSortOrder.ASC);
        }
    }
    private void LoadData(AJVMCompareMode mode, AJVMSortOrder sort)
    {
        // Added inverted conditional to escape method 
        // if the _containerId is invalid.
        if (_containerId <= 0) return;
        ///////////

        var alumniJobPostManager = new AlumniJobPostManager();

        // Whichever folder Id... 
        var jobs = alumniJobPostManager.GetList(_containerId);

        if (jobs != null && jobs.Any())
        {
            List<AlumniJobPostViewModel> lst = new List<AlumniJobPostViewModel>(jobs);
            AlumniJobPostViewModel.Mode = mode;
            AlumniJobPostViewModel.SortOrder = sort;
            lst.Sort();
            uxPhotoGallery.DataSource = lst;
            uxPhotoGallery.DataBind();
            lblCount.Text = "" + uxPhotoGallery.Items.Count;
        }
    }
    protected void lnkBtnCompany_Click(object sender, EventArgs e)
    {
        if (AlumniJobPostViewModel.Mode != AJVMCompareMode.Company || AlumniJobPostViewModel.SortOrder == AJVMSortOrder.DESC)
        {
            LoadData(AJVMCompareMode.Company, AJVMSortOrder.ASC);
        }
        else
        {
            LoadData(AJVMCompareMode.Company, AJVMSortOrder.DESC);
        }

    }
    protected void lnkBtnTitle_Click(object sender, EventArgs e)
    {
        if (AlumniJobPostViewModel.Mode != AJVMCompareMode.Title|| AlumniJobPostViewModel.SortOrder == AJVMSortOrder.DESC)
        {
            LoadData(AJVMCompareMode.Title, AJVMSortOrder.ASC);
        }
        else
        {
            LoadData(AJVMCompareMode.Title, AJVMSortOrder.DESC);
        }

    }
    protected void lnkBtnLocation_Click(object sender, EventArgs e)
    {
        if (AlumniJobPostViewModel.Mode != AJVMCompareMode.Location || AlumniJobPostViewModel.SortOrder == AJVMSortOrder.DESC)
        {
            LoadData(AJVMCompareMode.Location, AJVMSortOrder.ASC);
        }
        else
        {
            LoadData(AJVMCompareMode.Location, AJVMSortOrder.DESC);
        }

    }
}

视图模型:

using System;
using System.Collections;

namespace Ektron.Custom.ViewModels
{
    public enum AJVMCompareMode
    {
        Date,
        Company,
        Title,
        Location
    }
    public enum AJVMSortOrder
    {
        ASC,
        DESC
    }


    /// <summary>
    /// Provides the fields necessary to display a PressPhoto Smart Form to the site.
    /// </summary>
    public class AlumniJobPostViewModel : IComparable<AlumniJobPostViewModel>
    {
        static public AJVMCompareMode Mode { get; set; }
        static public AJVMSortOrder SortOrder { get; set; }
        public int CompareTo(AlumniJobPostViewModel other)
        {
            switch (Mode)
            {
                case AJVMCompareMode.Date:
                    if (SortOrder == AJVMSortOrder.ASC)
                    {
                        return (this.alumniJobDateSub.CompareTo(other.alumniJobDateSub));
                    }
                    else
                    {
                        return (other.alumniJobDateSub.CompareTo(this.alumniJobDateSub));
                    }
                case AJVMCompareMode.Company:
                    if (SortOrder == AJVMSortOrder.ASC)
                    {
                        return (this.alumniJobCompany.CompareTo(other.alumniJobCompany));
                    }
                    else
                    {
                        return (other.alumniJobCompany.CompareTo(this.alumniJobCompany));
                    }
                case AJVMCompareMode.Title:
                    if (SortOrder == AJVMSortOrder.ASC)
                    {
                        return (this.alumniJobTitle.CompareTo(other.alumniJobTitle));
                    }
                    else
                    {
                        return (other.alumniJobTitle.CompareTo(this.alumniJobTitle));
                    }
                case AJVMCompareMode.Location:
                    if (SortOrder == AJVMSortOrder.ASC)
                    {
                        return (this.alumniJobLocation.CompareTo(other.alumniJobLocation));
                    }
                    else
                    {
                        return (other.alumniJobLocation.CompareTo(this.alumniJobLocation));
                    }
            }
            return 0;
        }
        public string ContentUrl { get; set; }
        public long ContentId { get; set; }
        public bool alumniJobPostExtranet { get; set; }
        public string alumniJobDateSub { get; set; }
        public string alumniJobClientNum { get; set; }
        public string alumniJobContactAtt1 { get; set; }
        public string alumniJobContactAtt2 { get; set; }
        public string alumniJobTitle { get; set; }
        public string alumniJobCompany { get; set; }
        public string alumniJobLocation { get; set; }
        public string alumniJobDescription { get; set; }
        public string alumniJobCompanyName { get; set; }
        public string alumniJobContactName { get; set; }
        public string alumniJobContactEmail { get; set; }
        public string alumniJobContactPhone { get; set; }
        public AlumniJobPostViewModel()
        {

        }
        static AlumniJobPostViewModel()
        {
            Mode = AJVMCompareMode.Date;
            SortOrder = AJVMSortOrder.ASC;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您想要一种方法将这样的过滤器轻松集成到现有代码中,我会在DataBinding之前使用LINQ查询。

假设你可以获得价值,并在没有帮助的情况下将其提供给你的LoadData方法,那么让我们看一下将数据绑定到控件的代码。

List<AlumniJobPostViewModel> lst = new List<AlumniJobPostViewModel>(jobs);
AlumniJobPostViewModel.Mode = mode;
AlumniJobPostViewModel.SortOrder = sort;
lst.Sort();
uxPhotoGallery.DataSource = lst;
uxPhotoGallery.DataBind();
lblCount.Text = "" + uxPhotoGallery.Items.Count;

现在让我们假设我们有一个变量filterText,其中包含您要用来过滤数据集的字符串,以便它只包含与您的四个属性之一的匹配项:alumniJobDateSub,{ {1}},alumniJobCompanyNamealumniJobTitle

alumniJobLocation

假设列表不是太大并且你已经将它放在内存(缓存)中,那么这应该足以满足基本需求。

加了: 如果您确定要搜索的每个属性都具有值(不为空),则上述方法有效。如果其中一个为null,您将看到上面的错误。

要解决此问题,请更改该部分,以便每个测试都具有空检查以及IndexOf搜索:

List<AlumniJobPostViewModel> lst = new List<AlumniJobPostViewModel>(jobs);
AlumniJobPostViewModel.Mode = mode;
AlumniJobPostViewModel.SortOrder = sort;
lst.Sort();

// Filtering code
lst = lst.Where(x =>
    (x.alumniJobDateSub.IndexOf(filterText, 0, StringComparison.CurrentCultureIgnoreCase) > -1) ||
    (x.alumniJobCompanyName.IndexOf(filterText, 0, StringComparison.CurrentCultureIgnoreCase) > -1) ||
    (x.alumniJobTitle.IndexOf(filterText, 0, StringComparison.CurrentCultureIgnoreCase) > -1) ||
    (x.alumniJobLocation.IndexOf(filterText, 0, StringComparison.CurrentCultureIgnoreCase) > -1)).ToList();


uxPhotoGallery.DataSource = lst;
uxPhotoGallery.DataBind();
lblCount.Text = "" + uxPhotoGallery.Items.Count;