I've been messing around for a couple of hours trying to add the following two statements to the BuildItem method in the MenuAdapter class file:
if (item.Value != null && item.Value.Length > 0)
writer.WriteAttribute("id", item.Value);
if (item != Control.SelectedItem)
writer.WriteAttribute("class", "AspNet-Menu-Link");
else
writer.WriteAttribute("class", "AspNet-Menu-Link-Selected");
This does add the id and class, but it is displaying on my page rather than emitting the id and class in the html?
What am I doing incorrectly?
Here is the modified method I was trying:
private void BuildItem(MenuItem item, HtmlTextWriter writer)
{
Menu menu = Control as Menu;
if ((menu != null) && (item != null) && (writer != null))
{
writer.WriteLine();
writer.WriteBeginTag("li");
writer.WriteAttribute("class", item.ChildItems.Count > 0 ? "AspNet-Menu-WithChildren" : "AspNet-Menu-Leaf");
writer.Write(HtmlTextWriter.TagRightChar);
writer.Indent++;
writer.WriteLine();
if (item.NavigateUrl.Length > 0)
{
writer.WriteBeginTag("a");
writer.WriteAttribute("href", Page.ResolveUrl(item.NavigateUrl));
writer.WriteAttribute("class", "AspNet-Menu-Link");
if (item.Target.Length > 0)
{
writer.WriteAttribute("target", item.Target);
}
if (item.ToolTip.Length > 0)
{
writer.WriteAttribute("title", item.ToolTip);
}
else if (menu.ToolTip.Length > 0)
{
writer.WriteAttribute("title", menu.ToolTip);
}
writer.Write(HtmlTextWriter.TagRightChar);
writer.Indent++;
writer.WriteLine();
}
else
{
writer.WriteBeginTag("span");
writer.WriteAttribute("class", "AspNet-Menu-NonLink");
writer.Write(HtmlTextWriter.TagRightChar);
writer.Indent++;
writer.WriteLine();
}
if (item.ImageUrl.Length > 0)
{
writer.WriteBeginTag("img");
writer.WriteAttribute("src", Page.ResolveUrl(item.ImageUrl));
writer.WriteAttribute("alt", item.ToolTip.Length > 0 ? item.ToolTip : (menu.ToolTip.Length > 0 ? menu.ToolTip : item.Text));
writer.Write(HtmlTextWriter.SelfClosingTagEnd);
}
writer.Write(item.Text);
if (item.NavigateUrl.Length > 0)
{
writer.Indent--;
writer.WriteLine();
writer.WriteEndTag("a");
}
else
{
writer.Indent--;
writer.WriteLine();
writer.WriteEndTag("span");
}
if ((item.ChildItems != null) && (item.ChildItems.Count > 0))
{
BuildItems(item.ChildItems, false, writer);
}
if (item.Value != null && item.Value.Length > 0)
writer.WriteAttribute("id", item.Value);
if (item != Control.SelectedItem)
writer.WriteAttribute("class", "AspNet-Menu-Link");
else
writer.WriteAttribute("class", "AspNet-Menu-Link-Selected");writer.Indent--;
writer.WriteLine();
writer.WriteEndTag("li");
}
}