locked
StaticDisplayLevels and MaximumDynamicDisplayLevels RRS feed

  • Question

  • User179526911 posted

     Hi,

        I'm very new to ASP, so please point out anything wrong with the code below.  I really liked the CSS friendly adapters, but I required the use of StaticDisplayLevels and MaximumDynamicDisplayLevels.  After searching around the internet for code that used both without any luck I sat down and came up with this.  I've tested it with the template and seems to work fine.  I only modified the function BuildItem so you can just copy and paste over the existing function.  Enjoy.

     

            private void BuildItem(MenuItem item, HtmlTextWriter writer)
    {
    Menu menu = Control as Menu;
    //added for static and dynamic levels support
    int staticLevels = menu.StaticDisplayLevels;
    int dynamicLevels = menu.MaximumDynamicDisplayLevels;

    if (staticLevels == null)
    {
    staticLevels = 1;
    }
    if (menu.MaximumDynamicDisplayLevels == null)
    {
    dynamicLevels = 1;
    }

    if ((menu != null) && (item != null) && (writer != null))
    {
    writer.WriteLine();
    writer.WriteBeginTag("li");

    //modified for static and dynamic levels support
    string theClass = ((item.ChildItems.Count > 0) && (item.Depth >= (staticLevels - 1)) &&
    ((item.Depth - (staticLevels - 1)) < dynamicLevels)) ? "AspNet-Menu-WithChildren" : "AspNet-Menu-Leaf";
    string selectedStatusClass = GetSelectStatusClass(item);
    if (!String.IsNullOrEmpty(selectedStatusClass))
    {
    theClass += " " + selectedStatusClass;
    }
    writer.WriteAttribute("class", theClass);

    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Indent++;
    writer.WriteLine();

    if (((item.Depth < staticLevels) && (menu.StaticItemTemplate != null)) ||
    ((item.Depth >= staticLevels) && (menu.DynamicItemTemplate != null)))
    {
    writer.WriteBeginTag("div");
    writer.WriteAttribute("class", GetItemClass(menu, item));
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Indent++;
    writer.WriteLine();

    MenuItemTemplateContainer container = new MenuItemTemplateContainer(menu.Items.IndexOf(item), item);
    if ((item.Depth < staticLevels) && (menu.StaticItemTemplate != null))
    {
    menu.StaticItemTemplate.InstantiateIn(container);
    }
    else
    {
    menu.DynamicItemTemplate.InstantiateIn(container);
    }
    container.DataBind();
    container.RenderControl(writer);

    writer.Indent--;
    writer.WriteLine();
    writer.WriteEndTag("div");
    }
    else
    {
    if (IsLink(item))
    {
    writer.WriteBeginTag("a");
    if (!String.IsNullOrEmpty(item.NavigateUrl))
    {
    writer.WriteAttribute("href", Page.Server.HtmlEncode(menu.ResolveClientUrl(item.NavigateUrl)));
    }
    else
    {
    writer.WriteAttribute("href", Page.ClientScript.GetPostBackClientHyperlink(menu, "b" + item.ValuePath.Replace(menu.PathSeparator.ToString(), "\\"), true));
    }

    writer.WriteAttribute("class", GetItemClass(menu, item));
    WebControlAdapterExtender.WriteTargetAttribute(writer, item.Target);

    if (!String.IsNullOrEmpty(item.ToolTip))
    {
    writer.WriteAttribute("title", item.ToolTip);
    }
    else if (!String.IsNullOrEmpty(menu.ToolTip))
    {
    writer.WriteAttribute("title", menu.ToolTip);
    }
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Indent++;
    writer.WriteLine();
    }
    else
    {
    writer.WriteBeginTag("span");
    writer.WriteAttribute("class", GetItemClass(menu, item));
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Indent++;
    writer.WriteLine();
    }

    if (!String.IsNullOrEmpty(item.ImageUrl))
    {
    writer.WriteBeginTag("img");
    writer.WriteAttribute("src", menu.ResolveClientUrl(item.ImageUrl));
    writer.WriteAttribute("alt", !String.IsNullOrEmpty(item.ToolTip) ? item.ToolTip : (!String.IsNullOrEmpty(menu.ToolTip) ? menu.ToolTip : item.Text));
    writer.Write(HtmlTextWriter.SelfClosingTagEnd);
    }

    writer.Write(item.Text);

    if (IsLink(item))
    {
    writer.Indent--;
    writer.WriteEndTag("a");
    }
    else
    {
    writer.Indent--;
    writer.WriteEndTag("span");
    }

    }

    //modified for static and dynamic levels support
    if (item.Depth < (staticLevels - 1))
    {
    writer.Indent--;
    writer.WriteLine();
    writer.WriteEndTag("li");

    foreach (MenuItem item2 in item.ChildItems)
    {
    BuildItem(item2, writer);
    }
    }
    else
    {
    if ((item.ChildItems != null) && (item.ChildItems.Count > 0))
    {
    if ((item.ChildItems[0].Depth - (staticLevels - 1)) <= dynamicLevels)
    {
    BuildItems(item.ChildItems, false, writer);
    }
    }

    writer.Indent--;
    writer.WriteLine();
    writer.WriteEndTag("li");
    }
    }
    }
      
    Sunday, December 16, 2007 9:26 PM

All replies

  • User209095427 posted

     Thankss Alot, Here is update for "cssfriendly-16291" version:

    private void BuildItem(MenuItem item, HtmlTextWriter writer)
            {
                Menu menu = Control as Menu;
                //added for static and dynamic levels support
                int staticLevels = menu.StaticDisplayLevels;
                int dynamicLevels = menu.MaximumDynamicDisplayLevels;

                if (staticLevels == null)
                {
                    staticLevels = 1;
                }
                if (menu.MaximumDynamicDisplayLevels == null)
                {
                    dynamicLevels = 1;
                }

                if ((menu != null) && (item != null) && (writer != null))
                {
                    writer.WriteLine();
                    writer.WriteBeginTag("li");

                    //modified for static and dynamic levels support
                    string theClass = ((item.ChildItems.Count > 0) && (item.Depth >= (staticLevels - 1)) &&
                        ((item.Depth - (staticLevels - 1)) < dynamicLevels)) ? "AspNet-Menu-WithChildren" : "AspNet-Menu-Leaf";
                    //((OLD))
                    //string theClass = (item.ChildItems.Count > 0) ? "AspNet-Menu-WithChildren" : "AspNet-Menu-Leaf";
                    string selectedStatusClass = GetSelectStatusClass(item);
                    if (!String.IsNullOrEmpty(selectedStatusClass))
                    {
                        theClass += " " + selectedStatusClass;
                    }
                    writer.WriteAttribute("class", theClass);

                    writer.Write(HtmlTextWriter.TagRightChar);
                    writer.Indent++;
                    writer.WriteLine();

                    if (((item.Depth < menu.StaticDisplayLevels) && (menu.StaticItemTemplate != null)) ||
                            ((item.Depth >= menu.StaticDisplayLevels) && (menu.DynamicItemTemplate != null)))
                    {
                        writer.WriteBeginTag("div");
                        writer.WriteAttribute("class", GetItemClass(menu, item));
                        writer.Write(HtmlTextWriter.TagRightChar);
                        writer.Indent++;
                        writer.WriteLine();

                        MenuItemTemplateContainer container = new MenuItemTemplateContainer(menu.Items.IndexOf(item), item);
                        //added to solve the <a href='<%# Eval("Text")%>'> binding problem
                        //http://forums.asp.net/t/1069719.aspx
                        //http://msdn.microsoft.com/en-us/library/system.web.ui.control.bindingcontainer.aspx
                        //The BindingContainer property is the same as the NamingContainer property,
                        //except when the control is part of a template. In that case, the BindingContainer
                        //property is set to the Control that defines the template.
                        menu.Controls.Add(container);

                        if ((item.Depth < menu.StaticDisplayLevels) && (menu.StaticItemTemplate != null))
                        {
                            menu.StaticItemTemplate.InstantiateIn(container);
                        }
                        else
                        {
                            menu.DynamicItemTemplate.InstantiateIn(container);
                        }
                        container.DataBind(); //Databinding must occurs before rendering
                        container.RenderControl(writer);
                        writer.Indent--;
                        writer.WriteLine();
                        writer.WriteEndTag("div");
                    }
                    else
                    {
                        if (IsLink(item))
                        {
                            writer.WriteBeginTag("a");
                            if (!String.IsNullOrEmpty(item.NavigateUrl))
                            {
                                writer.WriteAttribute("href", Page.Server.HtmlEncode(menu.ResolveClientUrl(item.NavigateUrl)));
                            }
                            else
                            {
                                writer.WriteAttribute("href", Page.ClientScript.GetPostBackClientHyperlink(menu, "b" + item.ValuePath.Replace(menu.PathSeparator.ToString(), "\\"), true));
                            }

                            writer.WriteAttribute("class", GetItemClass(menu, item));
                            WebControlAdapterExtender.WriteTargetAttribute(writer, item.Target);

                            if (!String.IsNullOrEmpty(item.ToolTip))
                            {
                                writer.WriteAttribute("title", item.ToolTip);
                            }
                            else if (!String.IsNullOrEmpty(menu.ToolTip))
                            {
                                writer.WriteAttribute("title", menu.ToolTip);
                            }
                            writer.Write(HtmlTextWriter.TagRightChar);
                            writer.Indent++;
                            writer.WriteLine();
                        }
                        else
                        {
                            writer.WriteBeginTag("span");
                            writer.WriteAttribute("class", GetItemClass(menu, item));
                            writer.Write(HtmlTextWriter.TagRightChar);
                            writer.Indent++;
                            writer.WriteLine();
                        }

                        if (!String.IsNullOrEmpty(item.ImageUrl))
                        {
                            writer.WriteBeginTag("img");
                            writer.WriteAttribute("src", menu.ResolveClientUrl(item.ImageUrl));
                            writer.WriteAttribute("alt", !String.IsNullOrEmpty(item.ToolTip) ? item.ToolTip : (!String.IsNullOrEmpty(menu.ToolTip) ? menu.ToolTip : item.Text));
                            writer.Write(HtmlTextWriter.SelfClosingTagEnd);
                        }

                        writer.Write(item.Text);

                        if (IsLink(item))
                        {
                            writer.Indent--;
                            writer.WriteEndTag("a");
                        }
                        else
                        {
                            writer.Indent--;
                            writer.WriteEndTag("span");
                        }

                    }

                    //modified for static and dynamic levels support
                    if (item.Depth < (staticLevels - 1))
                    {
                        writer.Indent--;
                        writer.WriteLine();
                        writer.WriteEndTag("li");

                        foreach (MenuItem item2 in item.ChildItems)
                        {
                            BuildItem(item2, writer);
                        }
                    }
                    else
                    {
                        if ((item.ChildItems != null) && (item.ChildItems.Count > 0))
                        {
                            if ((item.ChildItems[0].Depth - (staticLevels - 1)) <= dynamicLevels)
                            {
                                BuildItems(item.ChildItems, false, writer);
                            }
                        }

                        writer.Indent--;
                        writer.WriteLine();
                        writer.WriteEndTag("li");
                    }
                }
            }

    Monday, October 13, 2008 3:49 PM
  • User1265453780 posted

    Nice work turkoid, I was needing this too, and your post saved me a lot of time.

    Thursday, November 13, 2008 6:00 PM
  • User933321414 posted

     This too saved me some time, thanks.

     Also, I made a few extra modifications to ensure that the correct css classes were added indicating if a child of the current item was selected, or if children of the current item were selected.

    In the former we need to ensure that then level 0 needs to NOT be assigned the ChildSelected class when a child of level 1 is selected.

    In the latter we need to ensure that when level 0 is selected the ParentSelected class is not assigned to ALL of the other items in the menu (rather, this will only work for levels 1 onward).

      

    private bool IsChildItemSelected(MenuItem item)
    {
    bool bRet = false;

    //modification: all pages are children of the home page
    //so don't assign this to the root node only to 'deeper' nodes
    //(to coincide with the modification to the BuildItem method for StaticLevels)

    Menu menu = Control as Menu;
    int staticLevels = menu.StaticDisplayLevels;
    if ((item != null) && (staticLevels > 1) && (item.Depth == (staticLevels - 2)))
    {
    // don't add anything
    // (yes, I know there's probably a more elegant way to do this
    // - please post, I'm not too proud to have my eyes opened)
    }
    else //original code used
    {
    if ((item != null) && (item.ChildItems != null))
    {
    bRet = IsChildItemSelected(item.ChildItems);
    }
    }
    //mod end

    return bRet;
    }
     

     and

      

    private bool IsParentItemSelected(MenuItem item)
    {
    bool bRet = false;

    //modification: all pages are children of the home page
    //and so everything will return true if item depth=0
    //so don't assign this to the root node only to 'deeper' nodes
    //(to coincide with the modification to the BuildItem method for StaticLevels)
    //if selected page is item.depth == 0 then don't assign item.Parent.Selected to anyone

    Menu menu = Control as Menu;
    int staticLevels = menu.StaticDisplayLevels;
    if ((item != null) && (staticLevels > 1) && (item.Depth == (staticLevels - 1)))
    {
    //don't add anything
    }
    else {//original code

    if ((item != null) && (item.Parent != null))
    {
    if (item.Parent.Selected)
    {
    bRet = true;
    }
    else
    {
    bRet = IsParentItemSelected(item.Parent);
    }
    }

    }
    return bRet;
    }
     

    hth

    Friday, February 13, 2009 1:50 PM