5
嘗試使用EF4向VS2010中的動態數據網站添加一些不同的過濾器(除了ForeignKey過濾器)。我可以添加新的過濾器模板,但是如何指定將爲我的模型中的每個屬性顯示哪個模板?爲動態數據網站(VS2010,EF4)添加自定義過濾器
感謝
嘗試使用EF4向VS2010中的動態數據網站添加一些不同的過濾器(除了ForeignKey過濾器)。我可以添加新的過濾器模板,但是如何指定將爲我的模型中的每個屬性顯示哪個模板?爲動態數據網站(VS2010,EF4)添加自定義過濾器
感謝
下面是如何做到這一點的步驟:
1)創建下DynamicData \過濾器所需的篩選器一個新的用戶控件。我創建了一個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屬性指向一個過濾器註釋您的屬性和你是好去:
using System;使用System.Collections的 ; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
命名空間Test.Model { 公共部分類資產 { #地區的原始屬性
public virtual int Id
{
get;
set;
}
[FilterUIHint("TextFilter")]
public virtual string Name
{
get;
set;
}
...
非常感謝! – 2013-07-23 22:45:28