StaticItemTemplate is not applied on postback



Hi all,

I am having a strange problem when using the ASP.NET (2.0) menu
I have a menu control that gets populated in the page load. I do not
check the PostBack value because I want to rebuild the Menu Control
each page visit.

The first time the Menu is built exactly as specified in the
StaticItemTemplate, however, each pressing the postback button gives
me an un-staticitemtemplated version of the menu.

IOW on postback and rebuild of the menu, the itemtemplate (and thus
styling) is removed or not even applied.

Can anyone help???

Here is the code to reproduce:

<%@ Page Language="C#" %>

<%@ Register Src="UserControls/Menu.ascx" TagName="Menu"
TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://">

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
Menu1.Items.Add(new MenuItem("hello world1"));
Menu1.Items.Add(new MenuItem("hello world2"));
Menu1.Items.Add(new MenuItem("hello world3"));
Menu1.Items.Add(new MenuItem("hello world4"));


<html xmlns="" >
<head runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<asp:Menu ID="Menu1" runat="server">
<asp:Button ID="Button1" runat="server" Text='<%#
Eval("Text") %>' />
<asp:Button runat="server" ID="btnButton" Text="Do a postback
to rebuild the menu" />




John MJ Gorter

I guess that when I add menuitems after a postback on, for example, a
button click. The templated controls are not created before the render
get's called.

I've created a new Menu Control and overrode the Render method so
I was absolutely sure that the new control tree is created...

I had to manually databind the templated instances, here the code:

[ToolboxData("<{0}:MyMenu runat='server'
public class MyMenu : Menu
public MyMenu()

protected override void Render(HtmlTextWriter writer)
// create the controls again

// databind the controls again to get the correct values
foreach (Control control in this.Controls)

// call the render in the base..

I did not like to apply these changes. I really would like to
implement a cleaner solution (based on standard Menu Control

Hope this solution works for anyone who might bump agains this


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question