/// <summary> /// Recursive function that renders a menu item at the correct /// indentation level. This is a private helper function. /// </summary> /// <param name="output">The HTML output stream.</param> /// <param name="item">Menu item.</param> /// <param name="depth">Indentation depth.</param> private void RecursiveRender(HtmlTextWriter output, MenuCompositeItem item, int depth) { if (depth > 0) // Skip root node { if (depth == 1) { output.Write("<li>"); // main menu } else { output.Write(@"<li class=""indented"">"); // sub menu } output.Write(@"<a href=""" + item.Link + @""">"); if (item.Item == SelectedItem) // selected item { output.Write(@"<span class=""selected"">" + item.Item + "</span>"); } else { output.Write(item.Item); // unselected item. } output.Write("</a>"); } // Recursively iterate over its children. for (int i = 0; i < item.Children.Count; i++) { RecursiveRender(output, item.Children[i], depth + 1); } }
/// <summary> /// Establishes the composite menu hierarchy which is present on all pages. /// </summary> protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Build the composite menu tree // This tree implements the Composite Design Pattern. MenuCompositeItem root = new MenuCompositeItem("root", null); MenuCompositeItem home = new MenuCompositeItem("home", ResolveUrl("~/Default.aspx")); MenuCompositeItem shop = new MenuCompositeItem("shopping", ResolveUrl("~/WebShop/Shopping.aspx")); MenuCompositeItem prod = new MenuCompositeItem("products", ResolveUrl("~/WebShop/Products.aspx")); MenuCompositeItem srch = new MenuCompositeItem("search", ResolveUrl("~/WebShop/Search.aspx")); MenuCompositeItem cart = new MenuCompositeItem("cart", ResolveUrl("~/WebShop/Cart.aspx")); MenuCompositeItem admn = new MenuCompositeItem("administration", ResolveUrl("~/WebAdmin/Admin.aspx")); MenuCompositeItem cust = new MenuCompositeItem("customers", ResolveUrl("~/WebAdmin/Customers.aspx")); MenuCompositeItem ordr = new MenuCompositeItem("orders", ResolveUrl("~/WebAdmin/Orders.aspx")); MenuCompositeItem auth; if (Request.IsAuthenticated) auth = new MenuCompositeItem("logout", ResolveUrl("~/Logout.aspx")); else auth = new MenuCompositeItem("login", ResolveUrl("~/Login.aspx")); shop.Children.Add(prod); shop.Children.Add(srch); shop.Children.Add(cart); admn.Children.Add(cust); admn.Children.Add(ordr); root.Children.Add(home); root.Children.Add(shop); root.Children.Add(admn); root.Children.Add(auth); TheMenuComposite.MenuItems = root; } }
/// <summary> /// Renders the entire menu structure. /// </summary> /// <param name="output">The HTML output stream</param> protected override void RenderContents(HtmlTextWriter output) { MenuCompositeItem root = this.MenuItems; output.Write(@"<div id=""navcontainer"">"); output.Write(@" <ul id=""navlist"">"); RecursiveRender(output, root, 0); output.Write(@" </ul>"); output.Write(@"</div>"); }
/// <summary> /// Recursive function that renders a menu item at the correct /// indentation level. This is a private helper function. /// </summary> /// <param name="output">The HTML output stream.</param> /// <param name="item">Menu item.</param> /// <param name="depth">Indentation depth.</param> private void RecursiveRender(HtmlTextWriter output, MenuCompositeItem item, int depth) { if (depth > 0) // Skip root node { if (depth == 1) output.Write("<li>"); // main menu else output.Write(@"<li class=""indented"">"); // sub menu output.Write(@"<a href=""" + item.Link + @""">"); if (item.Item == SelectedItem) // selected item output.Write(@"<span class=""selected"">" + item.Item + "</span>"); else output.Write(item.Item); // unselected item. output.Write("</a>"); } // Recursively iterate over its children. for (int i = 0; i < item.Children.Count; i++) { RecursiveRender(output, item.Children[i], depth + 1); } }