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

Adding CheckBoxes to RadMenu Dynamic Items

4 Answers 213 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Holly
Top achievements
Rank 1
Holly asked on 20 Aug 2015, 02:29 PM

Hi I have a RadMenu that I am populating dynamically through the code behind.  I need to add checkboxes to each child item that allows the user to make a selection or multiple selections in the menu.  Is it possible to do this?  Someone had suggested to me that I try and nest a RadComboBox inside of the RadMenu, but I'm having no luck with that.  I will paste my code below if it will help.

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <telerik:RadMenu ID="handsetMenu" runat="server">   
                        <Items>    
                                                
                        </Items>                                           
                    </telerik:RadMenu>
                    </ContentTemplate>
                </asp:UpdatePanel> 

 

Code Behind:

 

protected void createFilter(int categoryid)
    {
        List<int> productIds = new List<int>();
        DataRow[] productRow = CategoriesProductsData.Tables["Products"].Select("Category_ID = " + 573);

        productIds = productRow.Select(p => int.Parse(p["Product_ID"].ToString())).ToList();

        ITCProductService pService = new TCProductServiceClient();
        var productTuples = (pService.GetProductsAttributes(productIds));

        List<Tuple<int, CustomAttribute>> customAttributes = new List<Tuple<int, CustomAttribute>>();
        foreach (var productTuple in productTuples)
        {
            foreach (var attributeTuple in productTuple.m_Item2)
            {
                var customAttribute = new Tuple<int, CustomAttribute>(productTuple.m_Item1, new CustomAttribute(attributeTuple));
                customAttributes.Add(customAttribute);
            }

        }

        List<CustomAttributeCategory> categories = new List<CustomAttributeCategory>();

        var categoryList = customAttributes.Select(a => a.Item2).Select(a => a.Attribute.Category).GroupBy(a => a.AttributeCategoryId);

        var CatProdList = new List<CustomAttributeCategory>();

        foreach (var category in categoryList)
        {
            var CatProd = new CustomAttributeCategory();

            var prodIDList = from product in customAttributes
                             where product.Item2.Attribute.CategoryId == category.Key
                             select Tuple.Create(product.Item1, product.Item2);

            CatProd.Category = customAttributes.Select(a => a.Item2.Attribute.Category).Where(a => a.AttributeCategoryId == category.Key).FirstOrDefault();
            CatProd.ProdAttributesTuple = new List<Tuple<int, CustomAttribute>>();
            CatProd.ProdAttributesTuple = prodIDList.ToList();

            CatProdList.Add(CatProd);
        }

        foreach (var cat in CatProdList)
        {
            var itemCategory = new RadMenuItem(cat.Category.Name);
            
            handsetMenu.Items.Add(itemCategory);
            

            var option = cat.ProdAttributesTuple.GroupBy(a => a.Item2.Attribute.Value).ToList();
            foreach (var attr in option)
            {
                itemCategory.Items.Add(new RadMenuItem(attr.Key));              
                

            }
        }

    }


    protected void handsetMenu_ItemDataBound(object sender, RadMenuEventArgs e)
    {
        DataRowView row = (DataRowView)e.Item.DataItem;

    }


    public class CustomAttributeCategory
    {
        public AttributeCategoryModel Category { get; set; }
        public List<Tuple<int, CustomAttribute>> ProdAttributesTuple { get; set; }
    }

    public class CustomAttribute
    {
        public AttributeModel Attribute { get; set; }

        public List<int> ProductIds { get; set; }

        public CustomAttribute(AttributeModel attribute)
        {
            Attribute = attribute;
            ProductIds = new List<int>();
        }
    }​

4 Answers, 1 is accepted

Sort by
0
Holly
Top achievements
Rank 1
answered on 20 Aug 2015, 08:34 PM

So I think I'm almost there.  I realized that I needed to create a contenttemplate programatically and add the checkbox in the template.  But when I try to use it in my menu depending on where i put it, it replaces either my Parent or Child items with checkboxes.  How can I have both checkboxes and a Child Item with it?

Here's code.

class CustomContentTemplate : ITemplate
   {
       public void InstantiateIn(Control container)
       {
           CheckBox cb = new CheckBox();
           container.Controls.Add(cb);
       }

         private void cb_DataBinding(object sender, EventArgs e)
       {
           CheckBox target = (CheckBox)sender;
           RadMenuItem item = (RadMenuItem)target.BindingContainer;
           target.Text = item.Value;
       }
   }

  

 foreach (var cat in CatProdList)
        {
            var itemCategory = new RadMenuItem(cat.Category.Name);
                        
            handsetMenu.Items.Add(itemCategory);

                       

            var option = cat.ProdAttributesTuple.GroupBy(a => a.Item2.Attribute.Value).ToList();
            foreach (var attr in option)
            {               
                itemCategory.Items.Add(new RadMenuItem(attr.Key));

                foreach (RadMenuItem item in handsetMenu.Items)
                {
                    CustomContentTemplate template = new CustomContentTemplate();
                    template.InstantiateIn(item);
                    item.DataBind();
                }               
                
            }
                 ​

0
Aneliya Petkova
Telerik team
answered on 24 Aug 2015, 11:15 AM
Hi Holly,

Please check the following help article providing more information on how to add Text on the CheckBoxes inside RadMenu's ItemTemplate: You may also find useful "Accessing controls inside templates" help article: Hope this will be helpful.

Regards,
Aneliya Petkova
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Holly
Top achievements
Rank 1
answered on 25 Aug 2015, 01:48 PM

Aneliya 

 These don't resolve my issue.  I'm actually creating my template that contains my CheckBox in the code behind.  I can't create it in the HTML, as the menu is built dynamically.  I have the following code where I create my template, and then in a button click event I try to access the checkbox, but I get back the error, An exception of type 'System.NullReferenceException' occurred in App_Web_umdevm1i.dll but was not handled in user code.  I don't think I'm accessing the control inside the template correctly.  Do you have any other advice?  Thanks.

Additional information: Object reference not set to an instance of an object.

 class CustomContentTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            CheckBox cb = new CheckBox();

            cb.Text = "Text";

            cb.DataBinding += new EventHandler(cb_DataBinding);
            container.Controls.Add(cb);            
            
        }

        private void cb_DataBinding(object sender, EventArgs e)
        {
            CheckBox target = (CheckBox)sender;
            RadMenuItem item = (RadMenuItem)target.BindingContainer;
            target.Text = item.Text;                    
        }
    }

 

 

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        RadMenuItem item = handsetMenu.Items[0];
        CheckBox CheckItem = item.FindControl("cb") as CheckBox;
        CheckItem.Checked = false;  
           
    }

 

 

 

0
Aneliya Petkova
Telerik team
answered on 26 Aug 2015, 11:16 AM
Hi Holly,

Please note that RadMenuItem item = handsetMenu.Items[0]; will return first root node in your RadMenu. If this root node doesn't have CheckBox control, a null reference exception will be thrown. Also note that .FindControl method searches for a server control with the specified ID parameter.

I tried the provided code and everything is working fine at my side. In order to help you, I will need a sample runnable project where I can inspect your RadMenu with templates implementation and see why you get this error.

Regards,
Aneliya Petkova
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Menu
Asked by
Holly
Top achievements
Rank 1
Answers by
Holly
Top achievements
Rank 1
Aneliya Petkova
Telerik team
Share this question
or