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

Cannot set Checked items in RadComboBox in FilterTemplate

6 Answers 122 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 27 May 2014, 03:37 PM
I have a radGrid with a FilterTemplate containing a radComboBox with CheckBoxes=true.
I want to preselect a number of items in the radComboBox but that doesn't seem to work inside the filtertemplate (while it works just fine for the radComboBox outside a grid)
The grid columns and template are created in server side code, the data is loaded via a pagemethod.
I minimized my code down to this:

gridtest.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;
using System.Web.Services;
 
namespace SMTX.Common.Web {
    public partial class gridtest : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack)
                initGrid();
        }
 
        public void initGrid() {
            Forms.BL.EntityClasses.ViewEntity objView = Forms.BL.MyCode.ViewFactory.fetch(18);
            RadGrid1.AllowFilteringByColumn = true;
 
            GridBoundColumn objCol = new GridBoundColumn {
                AllowFiltering = true,
                DataField = "Field1",
                HeaderText = "Field1",
                DataType = Type.GetType("System.String"),
                FilterTemplate = new FilterTemplateCombo()
            };
            RadGrid1.Columns.Add(objCol);
 
            objCol = new GridBoundColumn {
                AllowFiltering = false,
                DataField = "Field2",
                HeaderText = "Field2",
                DataType = Type.GetType("System.String")
            };
            RadGrid1.Columns.Add(objCol);
 
            ScriptManager.RegisterStartupScript(this, this.GetType(), "CallGetView", @"setTimeout(function() {radGrid_Init();}  , 1000);", true);
        }
 
        [WebMethod]
        public static List<dataContainer> GetData(List<GridFilterExpression> p_lstFilterExpressions) {
            List<dataContainer> lstData = getDataForGrid(p_lstFilterExpressions);
            return lstData;
        }
 
 
        public static List<dataContainer> getDataForGrid(List<GridFilterExpression> p_lstFilterExpressions) {
            List<dataContainer> lstData = dataContainer.getTestData();
            List<String> lstFilterValues = null;
            if (p_lstFilterExpressions.Count > 0)
                lstFilterValues = p_lstFilterExpressions[0].FieldValue.Split(new []{"||"}, StringSplitOptions.RemoveEmptyEntries).ToList();
             
            if (lstFilterValues == null)
                return lstData;
            else
                return lstData.Where(r => lstFilterValues.Contains(r.Field1)).ToList();
        }
 
    }
 
    public class dataContainer {
        public Object Field1 { get; set; }
        public Object Field2 { get; set; }
 
        public static List<dataContainer> getTestData() {
            List<dataContainer> lstToRetrun = new List<dataContainer>();
            lstToRetrun.Add(new dataContainer { Field1 = "Name1", Field2 = "Name1field2" });
            lstToRetrun.Add(new dataContainer { Field1 = "Name2", Field2 = "Name2field2" });
            return lstToRetrun;
        }
 
    }
    public class FilterTemplateCombo : ITemplate {
        object m_objDataSource;
 
        public FilterTemplateCombo() {
            m_objDataSource = new[] { "Name1", "Name2" };
        }
 
        public void InstantiateIn(Control objContainer) {
            RadComboBox objCombobox = new RadComboBox();
            objContainer.Controls.Add(objCombobox);
            objCombobox.DataSource = m_objDataSource;
            objCombobox.DataBind();
            objCombobox.OnClientItemChecked = "RadGrid_ComboBoxMultiFilterCommand";
            objCombobox.CheckBoxes = true;
            foreach (RadComboBoxItem objItem in objCombobox.Items)
                objItem.Checked = true;
        }
    }
}

gridtest.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gridtest.aspx.cs" Inherits="SMTX.Common.Web.gridtest" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<html>
<body>
    <form id="form1" runat="server">
        <ajax:ToolkitScriptManager ID="MyToolkitScriptManager" runat="server" EnablePageMethods="true" />
        <telerik:RadGrid ID="RadGrid1" runat="server">
            <ClientSettings>
                <ClientEvents OnCommand="RadGrid_Command" />
            </ClientSettings>
        </telerik:RadGrid>
        <telerik:RadScriptBlock runat="server">
            <script type="text/javascript">
                function radGrid_Init() {
                    var tableView = $find("RadGrid1").get_masterTableView();
                    PageMethods.GetData(tableView.get_filterExpressions().toList(), RadGrid_Update);
                }
 
                function RadGrid_Command(sender, args) {
                    args.set_cancel(true);
                    var filterExpressions = sender.get_masterTableView().get_filterExpressions();
                    PageMethods.GetData(filterExpressions.toList(), RadGrid_Update);
                }
 
                function RadGrid_ComboBoxMultiFilterCommand(sender, args) {
                    var gridTableView = sender.get_parent().get_parent().get_masterTableView();
                    var arr_objItems = sender.get_checkedItems();
                    if (arr_objItems.length > 0) {
                        var strData = "";
                        for (var i = 0; i < arr_objItems.length; i++)
                            strData += "||" + arr_objItems[i].get_value();
                        gridTableView.filter("Field1", strData, Telerik.Web.UI.GridFilterFunction.Custom);
                    } else {
                        gridTableView.filter("Field1", "", Telerik.Web.UI.GridFilterFunction.NoFilter);
                    }
                }
 
                function RadGrid_Update(result) {
                    var tableView = $find("RadGrid1").get_masterTableView();
                    tableView.set_dataSource(result);
                    tableView.dataBind();
                }
            </script>
        </telerik:RadScriptBlock>
    </form>
</body>
</html>

I expect the radComboBox filter in Field1 to have it's 2 items checked, but that doesn't seem to be the case, although they were checked in the code.

6 Answers, 1 is accepted

Sort by
0
Tim
Top achievements
Rank 1
answered on 27 May 2014, 03:43 PM
I forgot to delete 1 line, it can be ignored.
Forms.BL.EntityClasses.ViewEntity objView = Forms.BL.MyCode.ViewFactory.fetch(18);


0
Tim
Top achievements
Rank 1
answered on 28 May 2014, 01:00 PM
Just for clarity:
The mentionned line can be ignored, but that doesn't solve the issue. The line was a remainder of my original code, and has nothing to do with this issue.
0
Kostadin
Telerik team
answered on 30 May 2014, 08:46 AM
Hi Tim,

I noticed that you are creating the filter template on Page_Load event handler. Note that the templates should be created on Page_Init event handler as in this case they will be recreated on each postback and it will be properly added in the ViewState. I would recommend you to examine the following help article which elaborates more on this matter.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Tim
Top achievements
Rank 1
answered on 30 May 2014, 11:56 AM
Kostadin,
Thanks for your reply, I'm actually aware of that, but as this is only a minimized version of my actual code, and the page doesn't do any postbacks (filtering is done using a pagemethod), I was able to use filters this way.
But no matter if I use the page_init or page_load in this example, it doesn't make a difference, the checkboxes are not initially checked.
0
Accepted
Kostadin
Telerik team
answered on 04 Jun 2014, 07:33 AM
Hello Tim,

I assume that the items are still not bound and that is the reason to be unchecked on initial load. Nevertheless a possible solution is to check them OnItemDataBound event handler as demonstrated in the following code snippet.
void grid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridFilteringItem)
    {
        GridFilteringItem filter = e.Item as GridFilteringItem;
        RadComboBox combobox = filter["Field1"].FindControl("RadComboBox1") as RadComboBox;
        foreach (RadComboBoxItem objItem in combobox.Items)
            objItem.Checked = true;
    }
}

Additionally I modified the sample which you provided and attached it to this forum thread.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Tim
Top achievements
Rank 1
answered on 06 Jun 2014, 02:05 PM
That did it!
Thanks
Tags
Grid
Asked by
Tim
Top achievements
Rank 1
Answers by
Tim
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or