ASP.NET动态数据文本搜索自定义过滤器模板 [英] ASP.NET Dynamic Data TextSearch Custom Filter Template

查看:84
本文介绍了ASP.NET动态数据文本搜索自定义过滤器模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现对所有基于文本搜索自定义过滤器模板并进入运行构造的查询问题。我一直在下面贴在这个博客但不知道如何更改执行的GetQueryable方法

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

查询。在博客上例中的前pression是,如果我输入的文本数据库列中的文本完全匹配其工作的平等。

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.

目前我使用的是新的QueryExtender功能与SearchEx pression控制沿但这需要创造我需要为文本搜索功能的所有表的几个自定义页面。我想通过创建自定义过滤器模板干这件事。任何帮助将大大AP preciated。

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.

推荐答案

在LINQ to SQL中,弦乐。载方法是你如何前preSS LIKE操作符。下面是你如何围绕打造LIKE操作员过滤模板的一个例子。在这个例子中,我们会给我们的自定义过滤器模板名称文本。

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

第一步是更新动态数据的元数据。注释所有你想要的是能够与<一个搜索的列href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.filteruihintattribute.aspx\">FilterUIHintAttribute像这样:

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")]

现在,我们需要创建文本筛选器模板。创建的 Text.ascx 的在过滤模板文件夹的用户控件(通常为〜/ DynamicData /过滤器):

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

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

这是我们提供了没有办法为用户投递的页面(因此,更新的结果)更新的文本过滤后通知。作为一个风格问题,我发现文本框控件,自动回发到糊涂,我发现这是多余的有一个单独的按钮,回传每个过滤器。相反,我喜欢的单个按钮添加到页面模板(例如,〜/ DynamicData / PageTemplates / List.aspx),允许用户回发的网页并更新结果。下面是相关摘录:

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.

这篇关于ASP.NET动态数据文本搜索自定义过滤器模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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