Telerik Grid Filtering with MS DropDownList Instead of Textbox

原创 2012年03月29日 00:28:27

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


十九、UI-Grid 水平滚动

原文:191 Horizontal Scrolling显示大量列的滚动代码: index.html
  • vesong87
  • vesong87
  • 2017年04月11日 09:36
  • 678

Telerik Kendo UI 那点事【3】GridView MVVM前后台联动服务端分页、分组、过滤(查询)、排序

中文化之后,我们开始具体使用kendo ui组件。经常开发系统的我,通常从最常用的控件用起,那就是表格控件GridView!现在的软件系统基本上就是标签框、文本框、选择框、树、表格堆砌而成。因此接触任...
  • supperman_009
  • supperman_009
  • 2014年09月25日 18:43
  • 10865

KendoUi中KendoDropDownList控件的使用——三级级联模块的实现

1. 应用需求    在权限系统开发中除了以上数据表关系的设计之外,比较麻烦的地方是级联模块在页面的展示,由于设计中最多是控制到三级,因此三级级联模块的展示、编辑等页面操作是需要解决的问题,这里采用...
  • houqingdong2012
  • houqingdong2012
  • 2014年12月17日 11:05
  • 3803

图解.Net Telerik 控件教程

第一部分 WinForm版本 Telerik 控件 第一章 安装配置     鄙人电脑上装有Winform版和asp.net ajax 版的Telerik控件。装好后的目录结构如下。 ...
  • bcbobo21cn
  • bcbobo21cn
  • 2015年03月01日 19:07
  • 3336

DropDownList 控件 可编辑 查询 绑定数据

使用FineUI框架 前台显示 达到如下效果 一段JS搞定
  • yuluozhaoyang
  • yuluozhaoyang
  • 2016年03月15日 13:51
  • 369

Telerik Kendo UI 那点事【2】中文化(汉化)

CSDN上有一个引路蜂软件的博客,里面有kendo ui的一般用法。它那里讲过的我这里就不再继续重复。 由于框架设计的需要,后台使用spring,前台使用kendo ui构建页面。为了加快快发速度以...
  • supperman_009
  • supperman_009
  • 2014年09月25日 17:38
  • 32428

asp.net中DropDownList控件各种属性研究汇总

.aspx代码如下: AutoPostBack="True" > ...
  • yayun0516
  • yayun0516
  • 2014年12月08日 10:02
  • 1750

[Asp.Net]4种方法把数据绑定到Dropdownlist

第一种,把Array数组绑到dropdownlist 程序代码string[] Month =new string[7]{ "January", "February", "March", "April...
  • david_520042
  • david_520042
  • 2015年04月28日 19:12
  • 2461

使用 Maven Profile 和 Filtering 打各种环境的包

每个项目都会有多套运行环境(开发,测试,正式等等),不同的环境配置也不尽相同(如jdbc.url),借助Jenkins和自动部署提供的便利,我们可以把不同环境的配置文件单独抽离出来,打完包后用对应环境...
  • Victor_Cindy1
  • Victor_Cindy1
  • 2016年08月27日 18:45
  • 666

asp.net(C#)中实现多个DropDownList及多个TextBox的多条件查询

这里是通过语句拼接来实现多条件查询的 主要代码: ...
  • yangmingxing980
  • yangmingxing980
  • 2015年01月28日 21:25
  • 1439
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Telerik Grid Filtering with MS DropDownList Instead of Textbox
举报原因:
原因补充:

(最多只允许输入30个字)