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.