C# 在ListView中应用筛选

C# 在ListView中应用筛选,c#,asp.net,entity-framework,listview,C#,Asp.net,Entity Framework,Listview,我有一个列表视图,我想在其中应用过滤功能 <asp:ListView runat="server" ID="CollegeDetailsView" DataKeyNames="CollegeID" ItemType="CollegeDataLibrary.CollegeDetail" AutoGenerateColumns="false" ItemPlaceholderID="CollegeItem" AllowPaging="true" AllowSorting=

我有一个列表视图,我想在其中应用过滤功能

<asp:ListView runat="server" ID="CollegeDetailsView"
    DataKeyNames="CollegeID" ItemType="CollegeDataLibrary.CollegeDetail"
    AutoGenerateColumns="false" ItemPlaceholderID="CollegeItem"
    AllowPaging="true" AllowSorting="true"
    SelectMethod="GetData" OnItemDataBound="CollegeDetailsView_ItemDataBound">
    <EmptyDataTemplate>
        There are no entries found for Colleges
    </EmptyDataTemplate>
    <LayoutTemplate>
GetFilteredData方法是:

 public List<CollegeDetail> GetFilteredData(string Created, string FDP)
    {
        using (CollegeDataEntities context = new CollegeDataEntities())
        {
            return context.CollegeDetails.Where(cs => cs.CreatedBy == Created && cs.FDP == FDP).ToList();
        }
    }
public List GetFilteredData(创建字符串,字符串FDP)
{
使用(CollegeDataEntities上下文=新CollegeDataEntities())
{
返回context.CollegeDetails.Where(cs=>cs.CreatedBy==Created&&cs.FDP==FDP.ToList();
}
}
上下文具有精确的值,但绑定后显示以下错误:


您看到这个错误是因为您试图用两种方式绑定ListView。您正在标记中使用模型绑定方法,并在代码隐藏中手动设置
DataSource

您可以尝试做的一件事是删除模型绑定,然后继续手动绑定。删除
ItemType
SelectMethod
,而只调用页面中的GetData\u Load或其他什么

protected void Page_Load(object sender, EventArgs e)
{
    CollegeDetailsView.DataSource = GetData();
    CollegeDetailsView.DataBind();
}
然后,假设筛选设置正确,其他筛选应按原样工作。您可以尝试的另一件事显然是相反的——只坚持模型绑定,而删除所有手动绑定。这是一个例子

 public List<CollegeDetail> GetFilteredData(string Created, string FDP)
    {
        using (CollegeDataEntities context = new CollegeDataEntities())
        {
            return context.CollegeDetails.Where(cs => cs.CreatedBy == Created && cs.FDP == FDP).ToList();
        }
    }
protected void Page_Load(object sender, EventArgs e)
{
    CollegeDetailsView.DataSource = GetData();
    CollegeDetailsView.DataBind();
}