为动态数据网站添加自定义过滤器(VS2010、EF4)

发布于 2024-08-28 10:31:11 字数 101 浏览 7 评论 0原文

尝试使用 EF4 将一些不同的过滤器(除了外键过滤器之外)添加到 VS2010 中的动态数据网站。我可以添加新的过滤器模板,但如何指定为模型中的每个属性显示哪个模板?

谢谢

Trying to add some different filters (in addition to the ForeignKey filter) to a Dynamic Data Website in VS2010 using EF4. I can add the new Filter templates, but how do I specify which template will get displayed for each property in my model?

Thanks

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

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

发布评论

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

评论(1

零時差 2024-09-04 10:31:11

以下是执行此操作的步骤:

1) 在 DynamicData\Filters 下为所需的过滤器创建一个新的 UserControl。我创建了一个 TextFilter.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TextFilter.ascx.cs" Inherits="Test.Prototype.Web.DynamicData.DynamicData.Filters.TextFilter" %>
<asp:TextBox runat="server" ID="TextBox1" AutoPostBack="true"  OnTextChanged="TextBox1_OnTextChanged" CssClass="DDFilter">
</asp:TextBox>

和后面的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

using System.ComponentModel.DataAnnotations;
using System.Linq;

using System.Linq.Expressions;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace Test.Prototype.Web.DynamicData.DynamicData.Filters
{
    public partial class TextFilter : System.Web.DynamicData.QueryableFilterUserControl
    {

        private const string NullValueString = "[null]";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override Control FilterControl
        {
            get
            {
                return TextBox1;
            }
        }


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

        public override IQueryable GetQueryable(IQueryable source)
        {
            string selectedValue = TextBox1.Text;
            if (String.IsNullOrEmpty(selectedValue))
            {
                return source;
            }

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

            return ApplyEqualityFilter(source, Column.Name, value);

        }

    }
}

然后在您的模型中,只需使用指向下一个过滤器的 FilterUIHint 属性注释您的属性,就可以开始了


使用系统集合;
使用 System.Collections.Generic;
使用 System.Collections.ObjectModel;
使用 System.Collections.Specialized;

使用 System.ComponentModel.DataAnnotations;

命名空间测试.模型
{
公共部分类资产
{
#region 原始属性

    public virtual int Id
    {
        get;
        set;
    }

    [FilterUIHint("TextFilter")]
    public virtual string Name
    {
        get;
        set;
    }

...

Here are the steps for how to do this:

1) Create a new UserControl for the filter you want under DynamicData\Filters. I created a TextFilter.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TextFilter.ascx.cs" Inherits="Test.Prototype.Web.DynamicData.DynamicData.Filters.TextFilter" %>
<asp:TextBox runat="server" ID="TextBox1" AutoPostBack="true"  OnTextChanged="TextBox1_OnTextChanged" CssClass="DDFilter">
</asp:TextBox>

and the code behind:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

using System.ComponentModel.DataAnnotations;
using System.Linq;

using System.Linq.Expressions;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace Test.Prototype.Web.DynamicData.DynamicData.Filters
{
    public partial class TextFilter : System.Web.DynamicData.QueryableFilterUserControl
    {

        private const string NullValueString = "[null]";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override Control FilterControl
        {
            get
            {
                return TextBox1;
            }
        }


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

        public override IQueryable GetQueryable(IQueryable source)
        {
            string selectedValue = TextBox1.Text;
            if (String.IsNullOrEmpty(selectedValue))
            {
                return source;
            }

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

            return ApplyEqualityFilter(source, Column.Name, value);

        }

    }
}

Then in your model, just annotate your properties with the FilterUIHint attribute pointing to the next filter and you're good to go:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

using System.ComponentModel.DataAnnotations;

namespace Test.Model
{
public partial class Asset
{
#region Primitive Properties

    public virtual int Id
    {
        get;
        set;
    }

    [FilterUIHint("TextFilter")]
    public virtual string Name
    {
        get;
        set;
    }

...

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