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