ASP.NET 动态数据 TextSearch 自定义过滤器模板

发布于 2024-09-17 19:34:30 字数 457 浏览 3 评论 0原文

我正在尝试为所有基于文本的搜索实现自定义过滤器模板,但在构建查询时遇到了问题。我一直在遵循 此博客< 上发布的说明/a> 但不确定如何更改 GetQueryable 方法来执行

WHERE columnAttribute LIKE '%something%'

查询。在博客的示例中,表达式是一个等式,如果我输入的文本与数据库列中的文本完全匹配,则该等式有效。

目前,我正在使用新的 QueryExtender 功能以及 SearchExpression 控件,但这需要为我需要文本搜索功能的所有表创建多个自定义页面。我想通过创建自定义过滤器模板来干燥它。任何帮助将不胜感激。

I'm trying to implement a custom filter template for all text based searches and running into problems constructing the query. I've been following the instructions posted on this blog but not sure how change the GetQueryable method to perform a

WHERE columnAttribute LIKE '%something%'

query. In the example on the blog the expression is an equality which works if the text I enter exactly matches the text in the database column.

At the moment I'm using the new QueryExtender feature along with the SearchExpression control but this requires creating several custom pages for all the tables I need text search functionality for. I would like to DRY this up by creating the custom filter template. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧 2024-09-24 19:34:30

在 LINQ to SQL 中,Strings.Contains 方法是表达LIKE 运算符。以下是如何构建过滤器模板的示例围绕 LIKE 运算符。在此示例中,我们将自定义过滤器模板命名为“Text”。

第一步是更新动态数据元数据。使用 FilterUIHintAttribute 像这样:

[FilterUIHint("Text")]

现在我们需要创建“文本”过滤器模板。 DynamicData/Filters”)中创建 Text.ascx 用户控件:

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

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

接下来创建后面的代码,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);
    }
}

在过滤器模板文件夹(通常为“~ / 我们没有为用户提供在更新文本过滤器后回发页面(从而更新结果)的方法。就风格而言,我发现自动回发的 TextBox 控件令人困惑,并且我发现使用单独的按钮来回发每个单独的过滤器是多余的。相反,我喜欢向页面模板添加一个按钮(例如“~/DynamicData/PageTemplates/List.aspx”),以允许用户回发页面并更新结果。以下是相关摘录:

<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>

这就是全部内容。用户现在应该能够搜索在指定列中包含文本片段的记录。

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.

盗心人 2024-09-24 19:34:30

您是否尝试过使用 jQuery 来处理过滤和搜索?

这里是一个很棒的教程,向您展示如何使用 jQuery 和 ASP.NET 过滤和排序数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文