This is a migrated thread and some comments may be shown as answers.

OnCommand not firing when filtering on custom GridTemplateColumn

1 Answer 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 20 Mar 2014, 05:49 PM
I have a custom template to handle true/false data points. The custom filter is a simple dropdown with a null/true/false values.

I have a client OnCommand function, and when I filter from any of my regular columns the function fires normally, however, when I filter from my custom column the page post back and the filter works, but the OnCommand function never fires. Here is my OnCommand function and my custom template:


function GridCommand(grid, args) {
    var command = args.get_commandName();
    console.log(command);
    if (command == "Filter") {
        console.log("Filtering data...");
    }
};

        private class CheckColumnTemplate : GridTemplateColumn
        {
            protected DropDownList DDl;

            protected override void SetupFilterControls(TableCell cell)
            {
                base.SetupFilterControls(cell);
                cell.Controls.RemoveAt(0);
                DDl = new DropDownList();
                DDl.EnableViewState = true;
                DDl.ViewStateMode = ViewStateMode.Enabled;
                DDl.ID = "ddl_" + this.DataField;
                ListItem blankItem = new ListItem("", "");
                ListItem trueItem = new ListItem("True", "1");
                ListItem falseItem = new ListItem("False", "0");
                DDl.Items.Add(blankItem);
                DDl.Items.Add(trueItem);
                DDl.Items.Add(falseItem);
                DDl.AutoPostBack = true;
                DDl.SelectedIndexChanged += new EventHandler(FilterCombo_SelectedIndexChanged);
                cell.Controls.AddAt(0, DDl);
                cell.Controls.RemoveAt(1);
                DDl.DataTextField = this.DataField;
                DDl.DataValueField = this.DataField;
            }

            protected void FilterCombo_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList DDl = (DropDownList)sender;
                GridFilteringItem filterItem = (GridFilteringItem)DDl.NamingContainer;
                string filterType = DDl.SelectedValue == "" ? "NoFilter" : "EqualTo";
                filterItem.FireCommandEvent("Filter", new Pair(filterType, DDl.NamingContainer.ToString()));
            }

            protected override void SetCurrentFilterValueToControl(TableCell cell)
            {
                ((DropDownList)cell.Controls[0]).SelectedValue = this.CurrentFilterValue;
            }

            protected override string GetCurrentFilterValueFromControl(TableCell cell)
            {
                string currentValue = ((DropDownList)cell.Controls[0]).SelectedValue;
                this.CurrentFilterFunction = currentValue == "" ? GridKnownFunction.NoFilter : GridKnownFunction.EqualTo;
                return currentValue;
            }
        }






1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 25 Mar 2014, 11:08 AM
Hello Robert,

Please note that when creating template columns for RadGrid programmatically you need to define a custom class that implements the ITemplate interface. You can then assign an instance of this class to the ItemTemplate or EditItemTemplate for the GridTemplateColumn. Have in mind that when creating template columns programmatically you must create the RadGrid entirely in the code-behind using the Page_Init event.

Check out this article that illustrates how the approach could be implemented.

Regards,
Viktor Tachev
Telerik
 

Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

 
Tags
Grid
Asked by
Robert
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or