New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Custom

Apart from the built-in field editors, the RadFilter also provides the option to use a custom one.

Defining custom field editors

When defining a custom field editor, you should inherit the RadFilterDataFieldEditor class and override the following methods:

  • CopySettings - it takes the base editor for aparameter. Set its properties here;

  • ExtractValues - return the value used for building the filter expression;

  • InitializeEditor - set up the controls in the editor here;

  • SetEditorValues - set the retained filter value to the control in the editor here.

You can find the code for the definition of a custom field editor with a RadComboBox in it below:

The names of the custom filter classes should differ from the names of the built-in filters ones.

C#
namespace CustomEditors
{
    public class RadCustomFilterDropDownEditor : RadFilterDataFieldEditor
    {
        protected override void CopySettings(RadFilterDataFieldEditor baseEditor)
        {
            base.CopySettings(baseEditor);
            var editor = baseEditor as RadCustomFilterDropDownEditor;
            if (editor != null)
            {
                DataSource = editor.DataSource;
                DataTextField = editor.DataTextField;
                DataValueField = editor.DataValueField;
            }
        }

        public override System.Collections.ArrayList ExtractValues()
        {
            ArrayList list = new ArrayList();
            list.Add(_combo.SelectedValue);
            return list;
        }

        public override void InitializeEditor(System.Web.UI.Control container)
        {
            _combo = new RadComboBox();
            _combo.ID = "MyCombo";
            _combo.DataTextField = DataTextField;
            _combo.DataValueField = DataValueField;
            _combo.DataSource = DataSource;           
            _combo.DataBind();
            container.Controls.Add(_combo);             
        }

        public override void SetEditorValues(System.Collections.ArrayList values)
        {
            if (values != null && values.Count > 0)
            {
                if (values[0] == null)
                    return;
                var item = _combo.FindItemByValue(values[0].ToString());
                if (item != null)
                    item.Selected = true;
            }
        }


        public string DataTextField
        {
            get
            {
                return (string)ViewState["DataTextField"] ?? string.Empty;
            }
            set
            {
                ViewState["DataTextField"] = value;
            }
        }
        public string DataValueField
        {
            get
            {
                return (string)ViewState["DataValueField"] ?? string.Empty;
            }
            set
            {
                ViewState["DataValueField"] = value;
            }
        }
        public DataTable DataSource
        {
            get
            {
                return (DataTable)ViewState["DataSource"] ?? new DataTable();
            }
            set
            {
                ViewState["DataSource"] = value;
            }
        }

        private RadComboBox _combo;
    }
}

Using custom field editors

When have a custom editor defined, you can use it in the field editors collection of the RadFilter after you have registered its namespace in the corresponding page and set it as an editor in the FieldEditorCreating event:

ASPNET
<%@ Register Namespace="CustomEditors" TagPrefix="custom" %>
ASPNET
<telerik:RadFilter RenderMode="Lightweight" runat="server" ID="RadFilter1" FilterContainerID="SqlDataSource1"
    OnFieldEditorCreating="RadFilter1_FieldEditorCreating" ExpressionPreviewPosition="Bottom">
    <FieldEditors>
        <custom:RadCustomFilterDropDownEditor FieldName="ShipCountry" DataTextField="ShipCountry"
            DataValueField="ShipCountry" />
    </FieldEditors>
</telerik:RadFilter>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="Select OrderID, OrderDate, ShipVia, ShipName, ShipAddress, ShipCity, ShipCountry FROM Orders">
</asp:SqlDataSource>