如何使用基于实体框架和Web表单的多个过滤器过滤网格视图。 [英] How to filter a grid view using multiple filters based on entity framework and web forms.

查看:58
本文介绍了如何使用基于实体框架和Web表单的多个过滤器过滤网格视图。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试为我正在做的项目选择ASP.NET。

我一直在建设我的解决方案基于VS 2017提供的Web窗体模板。



我设法使用

Hi All,

I am trying to pick ASP.NET for a project that I am doing.
I have been building my solution based on the the Web Forms template that VS 2017 provided.

I manage to do a databind with a gridview using the

SelectMethod

已提供。



我现在面临的问题是我正在尝试根据多个控件过滤我的数据。



我见过一种控制过滤的解决方案:

ASP.NET 4.5:在ASP.NET Web窗体中使用模型绑定进行过滤DotNetCurry

ASP.NET Web窗体模型绑定|令人讨厌的花絮

但它们似乎只指向一个控件。



我已经构建了一些控件来提供日期日期过滤和另外一个控件在日志中搜索。



我有一种感觉,将来我需要更多的资源或指导,因为我已经相当Web Forms,SPA及其背后的一些技术以及它们之间的区别让人感到困惑。



我尝试了什么:



transactionhistory.aspx

provided.

The problem I am facing now is that I am trying to filter my data based on more than one control.

I have seen some solutions for one control filtering:
ASP.NET 4.5: Filtering using Model Binding in ASP.NET Web Forms | DotNetCurry
ASP.NET Web Forms Model Binding | Geeky Tidbits
But they only seem to point to one control.

I have already build some controls to provide date for date filtering and one more control to search within the logs.

I have a feeling that in the future I will need more resources or maybe a guide as I am already quite bewildered between Web Forms, SPA and some of the technology behind them and how they differ.

What I have tried:

transactionhistory.aspx

<div class="form-group">
    <asp:Label runat="server" AssociatedControlID="LikeCategoryName" CssClass="control-label">Category Name: </asp:Label>
    <asp:TextBox ID="LikeCategoryName" runat="server" CssClass="form-control" />
</div>
...
<asp:GridView runat="server" ID="TransactionHistoryGrid"

    ItemType="transactionhistory" DataKeyNames="Id"

    SelectMethod="TransactionHistoryGrid_GetData"

    AllowSorting="true" AllowPaging="true" PageSize="10"

    AutoGenerateColumns="false" CssClass="table table-striped table-bordered" HeaderStyle-CssClass="thead-dark">
    <Columns>
        <asp:DynamicField DataField="DateTime" DataFormatString="{0:s}" HtmlEncode="false" />
        <asp:DynamicField DataField="CategoryName" />
        <asp:DynamicField DataField="Log" />
        <asp:TemplateField HeaderText="Status" SortExpression="Status">
            <ItemTemplate>
                <asp:Label runat="server" Text='<%# Boolean.Parse(Eval("Status").ToString()) ? "Successful" : "Failed" %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>





transactionhistory.aspx.cs



transactionhistory.aspx.cs

public IQueryable<transactionhistory> TransactionHistoryGrid_GetData([Control("LikeCategoryName")] String category)
{
    var query = entities.transactionhistories;
    if (category.length > 0)
    {
        query = query.Where(c => c.CategoryName.Contains(category));
    }
    return query;
}

推荐答案

您需要做的就是为您要筛选的每个控件的方法添加另一个参数:

All you need to do is add another parameter to your method for each control you want to filter on:
public IQueryable<transactionhistory> TransactionHistoryGrid_GetData(
    [Control("LikeCategoryName")] string category,
    [Control("SecondFilterControl")] string secondFilter,
    [Control("AnotherFilterHere")] int? anotherFilter)
{
    ...
}



然后应用任何对查询有价值的过滤器:


Then apply any filters which have a value to your query:

if (!string.IsNullOrEmpty(category))
{
    query = query.Where(c => c.CategoryName.Contains(category));
}
if (!string.IsNullOrEmpty(secondFilter))
{
    query = query.Where(c => c.SomeOtherField.StartsWith(secondFilter));
}
if (anotherFilter != null)
{
    DateTime minimumDate = DateTime.Today.AddDays(-anotherFilter.GetValueOrDefault());
    query = query.Where(c => c.SomeDateField >= minimumDate);
}



Visual Studio 2013中的模型绑定和Web表单| Microsoft Docs [ ^ ]


这篇关于如何使用基于实体框架和Web表单的多个过滤器过滤网格视图。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆