locked
Populating RadMenu from Code Behind RRS feed

  • Question

  • User-971051533 posted

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

    protected void handsetMenu_ItemDataBound(object sender, RadMenuEventArgs e)
    {
        var categories = Session["filter"];
    
        foreach (var category in categories)
        {
    
        }  
    
    foreach (RadMenuItem item in handsetMenu.Items)
        {
    
        }      
    }

    In the above function I'm trying to use my session to fill the control. In the first foreach I'm getting the error message: foreach statement can not foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'. Here is the rest of my code. If anyone could help me with this, it would be great. I'm totally lost. Thanks in advance.

    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>();
            }
        }
    
        protected void handsetMenu_ItemDataBound(object sender, RadMenuEventArgs e)
        {
            var categories = Session["filter"];
    
            foreach (var category in categories)
            {
    
            }
    
            foreach (RadMenuItem item in handsetMenu.Items)
            {
    
    }
        }
    
    
    
    <telerik:RadMenu ID="handsetMenu" runat="server" ShowToggleHandle="true" OnItemDataBound="handsetMenu_ItemDataBound">                    
                        <Items>
                            <telerik:RadMenuItem Text="Here">
                                <Items>
                                    
                                </Items>
                            </telerik:RadMenuItem>
                        </Items>                    
                    </telerik:RadMenu>   

     

    Monday, August 17, 2015 2:17 PM

Answers

  • User37182867 posted

    foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'.

    Cant find in your code where you are declaring categories. What ever categories is and I'm guessing its a list of some type or queryable. When you assign it to your session to do not use the ToString() just assign it.

    Session["filter"] = categories

    Then when you later assign Session variable back to a variable you need to cast it back to its original type.

    List<category> categories = (List<category>) Session["Filter"];

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 17, 2015 2:24 PM
  • User-971051533 posted

    Yes sorry about that.  I usually do.  The code that finally populated the menu is:

    foreach (var cat in categories)
            {
                //var itemCategory = new RadMenuItem(cat.Category.Name.ToString());
                var itemCategory = new RadMenuItem(cat.Category.Name);
    
                handsetMenu.Items.Add(itemCategory);
    
                foreach (var attr in cat.Attributes)
                {
                    itemCategory.Items.Add(new RadMenuItem(attr.Attribute.Value));
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 18, 2015 4:54 PM

All replies

  • User37182867 posted

    foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'.

    Cant find in your code where you are declaring categories. What ever categories is and I'm guessing its a list of some type or queryable. When you assign it to your session to do not use the ToString() just assign it.

    Session["filter"] = categories

    Then when you later assign Session variable back to a variable you need to cast it back to its original type.

    List<category> categories = (List<category>) Session["Filter"];

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 17, 2015 2:24 PM
  • User-971051533 posted

    Hi DeadTroll,

    I actually changed my code a little and the error message went away.  I'm not sure if I'm going in the right direction though.  The menu still isn't loading.  Here's what I've got now.

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

    I know I'm missing something in my RadMenu control too.  I'm really lost.  Frown

    <td valign="top" id="FilterInfoCollapse" class="navbar-collapse filter-nav collapse" aria-expanded="true">
                    <telerik:RadMenu ID="handsetMenu" runat="server" ShowToggleHandle="true" OnItemDataBound="handsetMenu_ItemDataBound">                    
                        <Items>
                            <telerik:RadMenuItem Text="Here">
                                <Items>
                                    
                                </Items>
                            </telerik:RadMenuItem>
                        </Items>                    
                    </telerik:RadMenu> 



    Monday, August 17, 2015 2:34 PM
  • User37182867 posted

    unfortunately I do not have access to Telerik like I used to so I can't really help you with it. Check on their forums though they have a pretty good community. I'm pretty sure you need some more markup in that control before it will process though... I've not done a lot with their radmenu so I don't know how much help I can be for you at this point.

    check this page its got some useful examples and documentation 

    http://demos.telerik.com/aspnet-ajax/menu/examples/overview/defaultcs.aspx

    Monday, August 17, 2015 2:44 PM
  • User-971051533 posted

    Well thanks for trying to help me.  I was avoiding the Telerik forum, because it takes them so long to get back to you, but I did just post there.  Does anyone else here have any ideas for me?

    Monday, August 17, 2015 2:59 PM
  • User-971051533 posted

    I actually ended up getting this figured out.  I had a few errors in my code that was keeping the menu from loading.

    Tuesday, August 18, 2015 12:03 PM
  • User37182867 posted

    If possible please post your corrected code so that others that find this thread know what you did to resolve your issue.

    Tuesday, August 18, 2015 1:16 PM
  • User-971051533 posted

    Yes sorry about that.  I usually do.  The code that finally populated the menu is:

    foreach (var cat in categories)
            {
                //var itemCategory = new RadMenuItem(cat.Category.Name.ToString());
                var itemCategory = new RadMenuItem(cat.Category.Name);
    
                handsetMenu.Items.Add(itemCategory);
    
                foreach (var attr in cat.Attributes)
                {
                    itemCategory.Items.Add(new RadMenuItem(attr.Attribute.Value));
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 18, 2015 4:54 PM