Populating RadMenu from Code Behind

3 posts, 0 answers
  1. Holly
    Holly avatar
    11 posts
    Member since:
    Jul 2015

    Posted 17 Aug 2015 Link to this post

    I  am trying to dynamically populate a Telerik Radmenu control in my code behind. I'm having a really hard time with it.

    I have the following code that I get a list of categories from and save to a Session Variable.

     protected void createFilter(int categoryid)
        {
            
             // check cateogyrid 
           
            //get list of proudct id
            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();

            //get attributes
            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 categoryTuples = customAttributes.Select(a => a.Item2).Select(a => a.Attribute.Category).Distinct();
            foreach (var categoryTuple in categoryTuples)
            {
                var category = new CustomAttributeCategory(categoryTuple);
                var attributeByCategory = customAttributes.Select(a => a.Item2).Where(b => b.Attribute.CategoryId == categoryTuple.AttributeCategoryId).Distinct();
                foreach (var attributeTuple in attributeByCategory)
                {
                    
                    var attribute = new CustomAttribute(attributeTuple.Attribute);
                    var attributeProductIds = customAttributes.Where(a => a.Item2.Attribute.AttributeId == attributeTuple.Attribute.AttributeId).Select(a => a.Item1).ToList();
                    attribute.ProductIds = attributeProductIds;

                    
                    category.Attributes.Add(attribute);    
                }
                categories.Add(category);
            }

            Session["filter"] = categories.ToString();        
        
        }

     

     public class CustomAttributeCategory
        {
            public AttributeCategoryModel Category { get; set; }
            public List<CustomAttribute> Attributes { get; set; }

            public CustomAttributeCategory(AttributeCategoryModel category)
            {
                Category = category;
                Attributes = new List<CustomAttribute>();

            }
        }

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

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

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

     

    Then I try to add the categories to the menu with this code:

     protected void handsetMenu_ItemDataBound(object sender, RadMenuEventArgs e)
        {
            var categories = Session["filter"].ToString();

            foreach (var category in categories)
            {
                var item = new RadMenuItem(categories);
                handsetMenu.Items.Add(item);
            }        
        }

     

    It's not working.  I know I'm missing something from my HTML to bind the control, but I'm not even sure if the code in my code behind is working.  Here's the control:

    <telerik:RadMenu ID="handsetMenu" runat="server" ShowToggleHandle="true" OnItemDataBound="handsetMenu_ItemDataBound">                    
                        <Items>
                            <telerik:RadMenuItem Text="Here">
                                <Items>
                                    
                                </Items>
                            </telerik:RadMenuItem>
                        </Items>                    
                    </telerik:RadMenu>

     

    I am really lost if anyone could help me that'd be great.

  2. Holly
    Holly avatar
    11 posts
    Member since:
    Jul 2015

    Posted 17 Aug 2015 in reply to Holly Link to this post

    Ok, my code is bringing back the categories I need to populate my menu, but the menu doesn't populate.  My code has changed so here is my code behind:

    protected void createFilter(int categoryid)
        {
            
             // check cateogyrid 
           
            //get list of proudct id
            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();

            //get attributes
            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 categoryTuples = customAttributes.Select(a => a.Item2).Select(a => a.Attribute.Category).Distinct();
            foreach (var categoryTuple in categoryTuples)
            {
                var category = new CustomAttributeCategory(categoryTuple);
                var attributeByCategory = customAttributes.Select(a => a.Item2).Where(b => b.Attribute.CategoryId == categoryTuple.AttributeCategoryId).Distinct();
                foreach (var attributeTuple in attributeByCategory)
                {
                    
                    var attribute = new CustomAttribute(attributeTuple.Attribute);
                    var attributeProductIds = customAttributes.Where(a => a.Item2.Attribute.AttributeId == attributeTuple.Attribute.AttributeId).Select(a => a.Item1).ToList();
                    attribute.ProductIds = attributeProductIds;

                    
                    category.Attributes.Add(attribute);    
                }
                categories.Add(category);
            }

            foreach (var cat in categories)
            {
                var item = new RadMenuItem();
                handsetMenu.Items.Add(item);               
            }
        
        }

        public class CustomAttributeCategory
        {
            public AttributeCategoryModel Category { get; set; }
            public List<CustomAttribute> Attributes { get; set; }

            public CustomAttributeCategory(AttributeCategoryModel category)
            {
                Category = category;
                Attributes = new List<CustomAttribute>();

            }
        }

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

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

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

     

    And here is my control.  I know I don't have the control set up properly.

    <telerik:RadMenu ID="handsetMenu" runat="server" ShowToggleHandle="true">                  
                                           
    </telerik:RadMenu>

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Holly
    Holly avatar
    11 posts
    Member since:
    Jul 2015

    Posted 17 Aug 2015 Link to this post

    I've updated my code again.

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

                    var itemAttribute = new RadMenuItem(cat.Attributes.ToString());
                    handsetMenu.Items.Add(itemAttribute);
                }  

     

     <telerik:RadMenu ID="handsetMenu" runat="server" ShowToggleHandle="true">                                  
                    </telerik:RadMenu>

     

    It still does not add the items to the menu.  :(

Back to Top