Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Custom Filters for Dynamic Data Website (VS2010, EF4)

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

like image 977
adamnickerson Avatar asked Apr 09 '10 13:04

adamnickerson


1 Answers

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

...

like image 168
adamnickerson Avatar answered Sep 27 '22 01:09

adamnickerson