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

Dynamically adding a template to an individual menu item

3 Answers 319 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Darrel
Top achievements
Rank 1
Darrel asked on 26 Feb 2013, 11:06 PM

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%">
    <Items>
        <telerik:RadMenuItem runat="server" Text="logo" PostBack="False">
            <ItemTemplate>
                <asp:Image ID="Image1" runat="server" AlternateText="<%$ AppSettings:menuimgalttext %>"
                    Height="<%$ AppSettings:menuimgheight %>" ImageUrl="<%$ AppSettings:menuimg %>"
                    Width="<%$ AppSettings:menuimgwidth %>" />
            </ItemTemplate>
        </telerik:RadMenuItem>
        <telerik:RadMenuItem runat="server"  PostBack="False" CssClass="rightMenu">
            <ItemTemplate>
                <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>
            </ItemTemplate>
        </telerik:RadMenuItem>
    </Items>
</telerik:RadMenu>


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
        container.Controls.Add(lit1)
    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%">
    <Items>
    </Items>
</telerik:RadMenu>


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.Items.Add(item6)
                RadMenu1.ItemTemplate = Nothing
 
                RadMenu1.DataBind()

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.

 

 

 

 

3 Answers, 1 is accepted

Sort by
0
Dimitar Terziev
Telerik team
answered on 27 Feb 2013, 08:04 AM
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.
0
Aaron
Top achievements
Rank 1
answered on 04 Apr 2018, 04:17 PM

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.Items.Add(item6)
                RadMenu1.ItemTemplate = Nothing

                RadMenu1.DataBind()

0
Rumen
Telerik team
answered on 05 Apr 2018, 08:13 AM
Hello Aaron,

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

Best regards,
Rumen
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.
Tags
Menu
Asked by
Darrel
Top achievements
Rank 1
Answers by
Dimitar Terziev
Telerik team
Aaron
Top achievements
Rank 1
Rumen
Telerik team
Share this question
or