Trả lời

9

Dưới đây là các bước để làm thế nào để làm điều này:

1) Tạo một UserControl mới cho bộ lọc mà bạn muốn dưới DynamicData \ Filters. Tôi tạo ra một 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> 

và mã đằng sau:

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

     } 

    } 
} 

Sau đó, trong mô hình của bạn, chỉ cần chú thích tài sản của bạn với các thuộc tính FilterUIHint trỏ đến bộ lọc tiếp theo và bạn tốt để go:

sử dụng Hệ thống; bằng System.Collections; bằng System.Collections.Generic; bằng System.Collections.ObjectModel; sử dụng System.Collections.Specialized;

sử dụng System.ComponentModel.DataAnnotations;

Thuộc tính

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

public virtual int Id 
    { 
     get; 
     set; 
    } 

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

...

+0

nhiều đánh giá cao! –

Các vấn đề liên quan