添加自定义过滤器的动态数据网站(VS2010,EF4) [英] Adding Custom Filters for Dynamic Data Website (VS2010, EF4)

查看:349
本文介绍了添加自定义过滤器的动态数据网站(VS2010,EF4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用EF4加入一些不同的过滤器(除ForeignKey的过滤器),以动态数据的网站中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?

感谢

推荐答案

下面是步骤,如何做到这一点:

Here are the steps for how to do this:

1)为要过滤下DynamicData \过滤器一个新的用户控件。我创建了一个TextFilter.ascx:

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>

和后面的code:

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属性指向下一个过滤器注释你的财产,你是好去:

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

使用系统; System.Collections中使用; 使用System.Collections.Generic; 使用System.Collections.ObjectModel; 使用System.Collections.Specialized;

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

使用System.ComponentModel.DataAnnotations;

using System.ComponentModel.DataAnnotations;

命名空间Test.Model {     公共部分类资产     {         #地区的原始属性。

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

    public virtual int Id
    {
        get;
        set;
    }

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

...

这篇关于添加自定义过滤器的动态数据网站(VS2010,EF4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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