关闭

Telerik Grid Filtering with MS DropDownList Instead of Textbox

标签: textboxstringasp.netlistobjectassembly
1034人阅读 评论(0) 收藏 举报
分类:

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


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:322002次
    • 积分:5271
    • 等级:
    • 排名:第5374名
    • 原创:186篇
    • 转载:252篇
    • 译文:0篇
    • 评论:3条