Telerik Grid Filtering with MS DropDownList Instead of Textbox

Reference:http://www.telerik.com/help/aspnet-ajax/grid-filtering-with-dropdownlist.html


1. Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="TelerikGridCustomFilteringColumn._Default" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register Namespace="TelerikGridCustomFilteringColumn" TagPrefix="custom" Assembly="TelerikGridCustomFilteringColumn"  %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">
            www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
                            <AjaxSettings>
                                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                                    <UpdatedControls>
                                        <telerik:AjaxUpdatedControl ControlID="RadGrid1"/>
                                    </UpdatedControls>
                                </telerik:AjaxSetting>
                            </AjaxSettings>
                        </telerik:RadAjaxManager>
    <telerik:RadGrid id="RadGrid1" runat="server" allowfilteringbycolumn="True" allowpaging="True"
        pagesize="5" onneeddatasource="RadGrid1_NeedDataSource">
         <MasterTableView
            AutoGenerateColumns="false"
            CommandItemDisplay="Top">
           <Columns>
             <custom:MyCustomFilteringColumn
                HeaderText="Employee ID"
                DataField="EmployeeID"
                UniqueName="EmployeeID" AutoPostBackOnFilter="true"/>
             <telerik:GridBoundColumn
                AllowFiltering="False"
                HeaderText="Customer ID"
                DataField="CustomerID"
                UniqueName="CustomerID" />
             <telerik:GridBoundColumn
                AllowFiltering="False"
                HeaderText="Order ID"
                DataField="OrderID"
                UniqueName="OrderID" />
           </Columns>
         </MasterTableView>
        </telerik:RadGrid>
</asp:Content>

2. Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace TelerikGridCustomFilteringColumn
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        private List<KeyValuePair<string, string>> GetFilterTable(string Field)
        {
            List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();

            KeyValuePair<string, string> a = new KeyValuePair<string, string>("Employee1", "C1");
            KeyValuePair<string, string> b = new KeyValuePair<string, string>("Employee2", "C2");
            KeyValuePair<string, string> c = new KeyValuePair<string, string>("Employee3", "C3");

            list.Add(a);
            list.Add(b);
            list.Add(c);

            return list;
        }

        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            foreach (GridBoundColumn column in RadGrid1.MasterTableView.Columns)
            {
                if (column is TelerikGridCustomFilteringColumn.MyCustomFilteringColumn)
                {
                    (column as TelerikGridCustomFilteringColumn.MyCustomFilteringColumn).ListDataSource = GetFilterTable(column.DataField);
                }
            }

            RadGrid1.DataSource = new List<Orders>()
            {
                new Orders("C1","aa","bb"),
                new Orders("C1","aa","bb"),
                new Orders("C2","aa","bb"),
                new Orders("C2","aa","bb"),
                new Orders("C2","aa","bb"),
                new Orders("C2","aa","bb")
            };
        }
    }

    public class Orders
    {
        public string EmployeeID {get;set;}
        public string CustomerID {get;set;}
        public string OrderID {get;set;}

        public Orders(string e, string c, string o)
        {
            EmployeeID = e;
            CustomerID = c;
            OrderID = o;
        }
    }
}

3. MyCustomFilteringColumn.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace TelerikGridCustomFilteringColumn
{
    public class MyCustomFilteringColumn : GridBoundColumn
    {
        private object listDataSource = null;
        //RadGrid calls this method when it initializes the controls inside the filtering item cells 
        protected override void SetupFilterControls(TableCell cell)
        {
            base.SetupFilterControls(cell);
            cell.Controls.RemoveAt(0);
            DropDownList list = new DropDownList();
            list.ID = "list" + this.DataField;
            list.AutoPostBack = true;
            list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
            cell.Controls.AddAt(0, list);
            cell.Controls.RemoveAt(1);
            list.DataTextField = "Key";
            list.DataValueField = "Value";
            list.DataSource = this.ListDataSource;
        }
        void list_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridFilteringItem filterItem = (sender as DropDownList).NamingContainer as GridFilteringItem;
            if (this.DataType == System.Type.GetType("System.Int32") || this.DataType == System.Type.GetType("System.Int16") || this.DataType == System.Type.GetType("System.Int64"))
            {
                filterItem.FireCommandEvent("Filter", new Pair("EqualTo", this.UniqueName));
            }
            else
                // treat everything else like a string   
                filterItem.FireCommandEvent("Filter", new Pair("Contains", this.UniqueName));
        }
        public object ListDataSource
        {
            get
            {
                return this.listDataSource;
            }
            set
            {
                listDataSource = value;
            }
        }
        //RadGrid calls this method when the value should be set to the filtering input control(s)  
        protected override void SetCurrentFilterValueToControl(TableCell cell)
        {
            base.SetCurrentFilterValueToControl(cell);
            DropDownList list = (DropDownList)cell.Controls[0];
            if (this.CurrentFilterValue != string.Empty)
            {
                list.SelectedValue = this.CurrentFilterValue;
            }
        }
        //RadGrid calls this method to extract the filtering value from the filtering input control(s)  
        protected override string GetCurrentFilterValueFromControl(TableCell cell)
        {
            DropDownList list = (DropDownList)cell.Controls[0];
            return list.SelectedValue;
        }
        protected override string GetFilterDataField()
        {
            return this.DataField;
        }
    }
}

Note: MyCustomFilteringColumn class "this.DataType"  will refer to data source data column field, if data field is int, then compare will use "EqualTo", but if data field is string, then compare will use "Contains"

void list_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridFilteringItem filterItem = (sender as DropDownList).NamingContainer as GridFilteringItem;
            if (this.DataType == System.Type.GetType("System.Int32") || this.DataType == System.Type.GetType("System.Int16") || this.DataType == System.Type.GetType("System.Int64"))
            {
                filterItem.FireCommandEvent("Filter", new Pair("EqualTo", this.UniqueName));
            }
            else
                // treat everything else like a string   
                filterItem.FireCommandEvent("Filter", new Pair("Contains", this.UniqueName));
        }


RadGrid1.DataSource = new List<Orders>()
            {
                new Orders("C1","aa","bb"),
                new Orders("C1","aa","bb"),
                new Orders("C2","aa","bb"),
                new Orders("C2","aa","bb"),
                new Orders("C2","aa","bb"),
                new Orders("C2","aa","bb")
            };


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值