Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / November 2007

Tip: Looking for answers? Try searching our database.

StaticItemTemplate is not applied on postback

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John123 - 30 Oct 2007 14:18 GMT
Hi all,

I am having a strange problem when using the ASP.NET (2.0) menu
control.
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://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
   protected void Page_Load(object sender, EventArgs e)
   {
       Menu1.Items.Clear();
       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"));
   }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
       <asp:Menu ID="Menu1" runat="server">
           <StaticItemTemplate>
               <asp:Button ID="Button1" runat="server" Text='<%#
Eval("Text") %>' />
           </StaticItemTemplate>
       </asp:Menu>
       <asp:Button runat="server" ID="btnButton" Text="Do a postback
to rebuild the menu" />

   </div>
   </form>
</body>
</html>
John MJ Gorter - 06 Nov 2007 15:35 GMT
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'
Text='Menu'></{0}:MyMenu>")]
   public class MyMenu : Menu
   {
       public MyMenu()
       {
       }

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

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

           // call the render in the base..
           base.Render(writer);
       }
   }

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

Hope this solution works for anyone who might bump agains this
situation.

Greets,
John.

>Hi all,
>
[quoted text clipped - 55 lines]
></body>
></html>

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.