Click here to Skip to main content
14,177,047 members
Rate this:
 
Please Sign up or sign in to vote.
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
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
<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
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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);
}

Model Binding and Web Forms in Visual Studio 2013 | Microsoft Docs[^]
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190526.1 | Last Updated 21 Mar 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100