Dynamically adding a template to an individual menu item

4 posts, 0 answers
  1. Darrel
    Darrel avatar
    2 posts
    Member since:
    Jul 2012

    Posted 26 Feb 2013 Link to this post

    Wow, this has been fun. I have a project that uses .net member and roles to dynamically build a menu. The first item in the menu and the last item in the menu were always the same so I initially crated a radmenu on my master page with the following:


    <telerik:RadMenu ID="RadMenu1" runat="server" Skin="Vista"
        OnClientItemClicked="onClickedMenuItem" Height="20px" Width="100%">
            <telerik:RadMenuItem runat="server" Text="logo" PostBack="False">
                    <asp:Image ID="Image1" runat="server" AlternateText="<%$ AppSettings:menuimgalttext %>"
                        Height="<%$ AppSettings:menuimgheight %>" ImageUrl="<%$ AppSettings:menuimg %>"
                        Width="<%$ AppSettings:menuimgwidth %>" />
            <telerik:RadMenuItem runat="server"  PostBack="False" CssClass="rightMenu">
                    <asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/Default.aspx?logout=true">Logout: </asp:LinkButton>
                    <asp:Label ID="lblUser" Text = "lblUser" runat="server"></asp:Label>
                    <asp:Label ID="lblInstitution" Text="lblInstitution" runat="server"></asp:Label>
                    <asp:Label ID="lbldate" Text="lbldate" runat="server"></asp:Label>

    As you can see, the two template items were the only items in the menu. I then used Radmenu1.Insert(1,“Menu Item”) to put items between these templates. This works great unless there is a postback  in which case the radmenu items of the two initial templates are instantiated without the templates (On initial display the page works just fine, only on a postback is there a problem). Telerik told me it had to do with the viewstate and how insert worked and suggested I use the information from the following article (
    http://www.telerik.com/help/aspnet-ajax/menu-templates-runtime.html)  to dynamically insert my templates.

     The deal with the article is it assumes the template is going to be applied to every item in the menu,  which for me is not the case. Based on article I created me a litTemplate class:

    Imports Telerik.Web.UI
    Public Class litTemplate
        Implements ITemplate
        Dim myId As String
        Public Sub New(ByVal id As String)
            myId = id
        End Sub
        Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
            Dim lit1 As New LiteralControl
            lit1.ID = myId
            AddHandler lit1.DataBinding, AddressOf lit1_DataBinding
        End Sub
        Private Sub lit1_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
            Dim target As LiteralControl = DirectCast(sender, LiteralControl)
            Dim item As RadMenuItem = DirectCast(target.BindingContainer, RadMenuItem)
            Dim itemText As String = DirectCast(DataBinder.Eval(item, "Text"), String)
            target.Text = itemText
        End Sub
    End Class

    It uses the new function to set the id for the template item.  Then, it uses the Text property from the radmenuitem to determine what the literal html to be inserted is.  

     I changed radmenu definition to:

    <telerik:RadMenu ID="RadMenu1" runat="server" Skin="Vista"
        OnClientItemClicked="onClickedMenuItem" Height="20px" Width="100%">

    I then used the following code at the end of my menu to always add the user login information with a Logout link:

    Dim litString as String = "<a href=""Default.aspx?logout=true"">Logout :</a> " + auser.UserName.ToString + " / " + Session("Institution") + " " + Session("ProcessDate")
                    Dim item6template As New litTemplate("lit1")
                    RadMenu1.ItemTemplate = item6template
                    Dim item6 As New RadMenuItem(litString)
                    RadMenu1.ItemTemplate = Nothing

    This creates the literal text template and sets the itemtemplate for the radmenu to the template. Now when a new radmenuitem is added it uses the template.  I then set it back to nothing so additional radmenuitems do not use the template. 

    So I guess the trick is just setting the Radmenu.ItemTemplate before adding an item then setting it back to nothing when done.  It is a pretty simple way to use templates on a  per radmenuitem basis if your dynamically building your menus.

    This litTemplate is flexible enough that I also used it to handle the logo as the first element of the menu. Using this technique, you could "literally", pun intended, insert anything in the menu.

    Hope this helps someone.





  2. Dimitar Terziev
    Dimitar Terziev avatar
    1268 posts

    Posted 27 Feb 2013 Link to this post

    Hello Darrel,

    Thank you for sharing this solution with the community, as a token of gratitude your Telerik points are updated.

    All the best,
    Dimitar Terziev
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. Aaron
    Aaron avatar
    5 posts
    Member since:
    Sep 2015

    Posted 04 Apr 2018 in reply to Darrel Link to this post

    Is this code still valid for the latest versions of the RadMenu?  If so, where are you putting this code?


    Dim litString as String = "<a href=""Default.aspx?logout=true"">Logout :</a> " + auser.UserName.ToString + " / " + Session("Institution") + " " + Session("ProcessDate")
                    Dim item6template As New litTemplate("lit1")
                    RadMenu1.ItemTemplate = item6template
                    Dim item6 As New RadMenuItem(litString)
                    RadMenu1.ItemTemplate = Nothing


  4. Rumen
    Rumen avatar
    14320 posts

    Posted 05 Apr 2018 Link to this post

    Hello Aaron,

    For example, you can put it in the Page_Load server event.

    Best regards,
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top