Building RAD Menu Items in code behind

2 posts, 0 answers
  1. cdikmen
    cdikmen avatar
    25 posts
    Member since:
    Aug 2006

    Posted 03 May 2009 Link to this post

    Here is what I am trying to do:

    1. I create RAD Menu items from data dynamically through code
    2. I would like to cache the RadMenu or the RadMenuItems so that I can efficiently apply the cached version to the live version in PageLoad of my master page.
    3. In 2006 version, I create a RadMenuItemCollection and placed it in the cache, then re-called it and looped through each item in the collection. That does not work in Q1 2008 version.


    Here is how it makes sense to me, but for some reason, it does not work


    Dim RMC as RadMenuItemCollection = Cache("RadMenuItems")
    Dim RM1 as RadMenuItem = RadMenu1.Items(1)

    For each item as RadMenuItem in RMC

    RM1.Items.Add(item)

    Next

    -----  SECOND ATTEMPT ---

    So then I tried to simply cache the RadMenuItem (with all the Items defined) as follows

                    Dim DT As DataTable = CacheObj.GetData

                    Dim RM1 As RadMenuItem = RMMaster.Items(1)

                    For Each row As DataRow In DT.Rows

                        Dim RMItem As New Telerik.Web.UI.RadMenuItem
                        RMItem .Text = row("Name").ToString
                        RMItem .NavigateUrl = "TestPage.aspx?cl=" + row("Id").ToString

                        RM1.Items.Add(RMItem)

                    Next

                    Dim RMC As Telerik.Web.UI.RadMenuItem = RM1 'RMC is the Item I am going to cache and recall later

                    Cache.Insert("RMC", RMC, Nothing, Now.AddHours(4), TimeSpan.Zero)

    'NEXT, I NEED TO RECALL THE MENU ITEM FROM THE CACHE, LOOP THROUGH THE ITEMS AND RE-ASSIGN TO THE MENU

                    Dim RMC As RadMenuItem = CType(Cache("RMC"), Telerik.Web.UI.RadMenuItem)
                    Dim RM1 As RadMenuItem = RMMaster.Items(1)
                    RM1.Items.Clear()

                    For Each item As Telerik.Web.UI.RadMenuItem In RMC.Items

                        RM1.Items.Add(item) ' On the second iteration I get the error here

                    Next

    When I trace through the code I check the RMC.Items.Count() property. It starts out at 29, which is correct, then after the first time through the For Loop, it decrements to 28. As if an item is being removed from the cached RadMenuItem. I don't get this at all! The same thing happens when I try to cache and loop through a RadMenuItemCollection.

  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 04 May 2009 Link to this post

    Hello cdikmen,

    It is generally not a good practice to store UI objects in Session or Cache. This leads to various problems such as memory leaks and odd behavior.

    One way to cache a menu is to call its GetXml method and store the resul (the XML representation of the menu) in Session or Cache. Then you can use the LoadXml method to populate the menu.

    Regards,
    Albert
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top