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");
}
}
}