ASP.NET Dynamic Data TextSearch Custom Filter Template

别来无恙 提交于 2019-11-30 19:26:52

In LINQ to SQL, the Strings.Contains method is how you express the LIKE operator. What follows is an example of how you could build a filter template around the LIKE operator. For this example, we'll give our custom filter template the name "Text".

The first step is to update the Dynamic Data metadata. Annotate all the columns you want to be able to search on with the FilterUIHintAttribute like so:

[FilterUIHint("Text")]

Now we need to create the "Text" filter template. Create the Text.ascx user control in the filter templates folder (typically "~/DynamicData/Filters"):

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Text.ascx.cs" Inherits="Text" %>

<asp:TextBox runat="server" ID="TextBox1" autocomplete="off" OnTextChanged="TextBox1_Changed" />

Next create the code behind, Text.ascx.cs:

public partial class Text : QueryableFilterUserControl {
    public override Control FilterControl {
        get { return TextBox1; }
    }

    protected void TextBox1_Changed(object sender, EventArgs e) {
        OnFilterChanged();
    }

    public override IQueryable GetQueryable(IQueryable source) {
        var value = TextBox1.Text;
        if (String.IsNullOrWhiteSpace(value)) return source;

        if (DefaultValues != null) {
            DefaultValues[Column.Name] = value;
        }

        var parameter = Expression.Parameter(source.ElementType);
        var columnProperty = Expression.PropertyOrField(parameter, Column.Name);
        var likeValue = Expression.Constant(value, typeof (string));
        var condition = Expression.Call(
            columnProperty,
            typeof (string).GetMethod("Contains"),
            likeValue);
        var where = Expression.Call(
            typeof (Queryable),
            "Where",
            new[] { source.ElementType },
            source.Expression,
            Expression.Lambda(condition, parameter));
        return source.Provider.CreateQuery(where);
    }
}

Notice that we have provided no way for the user to postback the page (and hence, update the results) after updating the Text filter. As a matter of style, I find TextBox controls that auto-postback to be confusing, and I find that it is redundant to have a separate button to postback each individual filter. Instead, I like to add a single Button to the page template (for example, "~/DynamicData/PageTemplates/List.aspx") that allows the user to postback the page and update the results. Here is the relevant excerpt:

<asp:Panel runat="server" DefaultButton="UpdateFilter">
    <asp:QueryableFilterRepeater runat="server" ID="FilterRepeater">
        <ItemTemplate>
            <asp:Label runat="server" Text='<%# Eval("DisplayName") %>' OnPreRender="Label_PreRender" />
            <asp:DynamicFilter runat="server" ID="DynamicFilter" OnFilterChanged="DynamicFilter_FilterChanged" /><br />
        </ItemTemplate>
    </asp:QueryableFilterRepeater>
    <asp:Button runat="server" ID="UpdateFilter" Text="Search" />
</asp:Panel>

That's all there is to it. Users should now be able to search for records that contain fragments of text in the specified columns.

Mysteric

Have you tried to use jQuery to handle the filtering and searching?

Here is a great tutorial that shows you how to filter and sort data with jQuery and ASP.NET.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!