Home All Groups Group Topic Archive Search About

DynamicItemTemplate not applied on programmatically added Menu Items

Author
4 Nov 2008 12:53 PM
ErwinP
Hello,

I'm having a bit of a problem with the ASP.NET Menu Web Control when
using a Dynamic Item Template. The template works fine for Menu Items
which are added using declarative persistence (Example 1). However
Menu Items which I add programmatically (Example 2) initially do not
receive the templated controls.
They are being displayed as 'ordinary' Menu Items until the next
PostBack when the template kicks in.

I've used Reflector to get a better understanding of how templates are
handled inside the Menu control and and my first impression is that
the Menu Control doesn't properly support templates for
programmatically added Menu Items. But as said; that's my first
impression so I really hope someone will prove me wrong! :-)

Best regards,

Erwin


Example 1:

    <asp:Menu runat="server" ID="PlaybackControl"
Orientation="Horizontal"
        OnMenuItemClick="PlaybackControl_MenuItemClick">
        <DynamicItemTemplate>
            <asp:Label runat="server" ID="MenuText"><%# Eval("Text") %></
asp:Label>
            <br />
            <asp:Label runat="server" ID="MenuDesc"><%# Eval("ToolTip") %></
asp:Label>
        </DynamicItemTemplate>
        <Items>
            <asp:MenuItem Text="Playback control">
                <asp:MenuItem Text="Play" ToolTip="Start playback" />
                <asp:MenuItem Text="Pause" ToolTip="Pause playback" />
                <asp:MenuItem Text="Stop" ToolTip="Stop playback" />
            </asp:MenuItem>
        </Items>
    </asp:Menu>
    <br />
    <asp:Button runat="server" ID="AddItem" Text="Add item"
OnClick="AddItem_Click" />

Example 2:

    private void AddItem_Click(object sender, EventArgs e)
    {
        MenuItem newItem = new MenuItem();
        newItem.Text = "New item";
        newItem.ToolTip = "Newly added dynamic menu item";
        PlaybackControl.Items[0].ChildItems.Add(newItem);
    }

Author
11 Nov 2008 2:14 PM
ErwinP
Ok after reverse engineering the Menu Web Control I've concluded that
dynamic item templates simply won't work with menu items which are
programmatically added to the menu as described. I did however also
discover that menu items created using data binding will get the
template so by using a different approach I get the same result:
- generate and maintain XML representation of the menu and all it's
items
- load XML into XmlDataSource control
- bind XmlDataSource control to the menu