Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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
HTML
SelectMethod
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
ASP.NET
<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
C#
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;
}
Posted
Updated 21-Mar-18 10:43am

1 solution

All you need to do is add another parameter to your method for each control you want to filter on:
C#
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:
C#
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);
}

Model Binding and Web Forms in Visual Studio 2013 | Microsoft Docs[^]
 
Share this answer
 
Comments
amsga 25-Mar-18 22:51pm    
Thank you Richard, it works like a charm. Now I just have to figure out how to trigger the postback from a button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900