使用 C# 按 TextBox 过滤 ListView
Filter ListView by TextBox with C#
我有一个显示作业列表的 ListView 控件。我有一行 LinkButton 控件,当单击该控件时,它会按作业的各个字段对列表进行排序。
我在页面上还有一个 TextBox 控件和 LinkButton 控件。
我想在 TextBox 中输入关键字来筛选 ListView。单击 LinkButton 控件后,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;
}
}
}
如果您想要一种轻松地将此类过滤器集成到现有代码中的方法,我会在 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
, alumniJobCompanyName
, alumniJobTitle
, alumniJobLocation
.
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;
假设列表不是太大并且您已经将其保存在内存(缓存)中,这应该足以满足基本需求。
已添加:
如果您确定要搜索的每个属性都有一个值(不为空),则上述方法有效。如果其中之一为空,您将看到上面的错误。
要纠正此问题,请更改该部分,以便每个测试都有空检查以及 IndexOf 搜索:
if (!string.IsNullOrEmpty(filterText))
{
lst = lst.Where(x =>
(x.alumniJobDateSub != null && x.alumniJobDateSub.IndexOf(filterText, 0, StringComparison.CurrentCultureIgnoreCase) > -1) ||
(x.alumniJobCompanyName != null && x.alumniJobCompanyName.IndexOf(filterText, 0, StringComparison.CurrentCultureIgnoreCase) > -1) ||
(x.alumniJobTitle != null && x.alumniJobTitle.IndexOf(filterText, 0, StringComparison.CurrentCultureIgnoreCase) > -1) ||
(x.alumniJobLocation != null && x.alumniJobLocation.IndexOf(filterText, 0, StringComparison.CurrentCultureIgnoreCase) > -1)).ToList();
}
我有一个显示作业列表的 ListView 控件。我有一行 LinkButton 控件,当单击该控件时,它会按作业的各个字段对列表进行排序。
我在页面上还有一个 TextBox 控件和 LinkButton 控件。
我想在 TextBox 中输入关键字来筛选 ListView。单击 LinkButton 控件后,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;
}
}
}
如果您想要一种轻松地将此类过滤器集成到现有代码中的方法,我会在 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
, alumniJobCompanyName
, alumniJobTitle
, alumniJobLocation
.
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;
假设列表不是太大并且您已经将其保存在内存(缓存)中,这应该足以满足基本需求。
已添加: 如果您确定要搜索的每个属性都有一个值(不为空),则上述方法有效。如果其中之一为空,您将看到上面的错误。
要纠正此问题,请更改该部分,以便每个测试都有空检查以及 IndexOf 搜索:
if (!string.IsNullOrEmpty(filterText))
{
lst = lst.Where(x =>
(x.alumniJobDateSub != null && x.alumniJobDateSub.IndexOf(filterText, 0, StringComparison.CurrentCultureIgnoreCase) > -1) ||
(x.alumniJobCompanyName != null && x.alumniJobCompanyName.IndexOf(filterText, 0, StringComparison.CurrentCultureIgnoreCase) > -1) ||
(x.alumniJobTitle != null && x.alumniJobTitle.IndexOf(filterText, 0, StringComparison.CurrentCultureIgnoreCase) > -1) ||
(x.alumniJobLocation != null && x.alumniJobLocation.IndexOf(filterText, 0, StringComparison.CurrentCultureIgnoreCase) > -1)).ToList();
}