Listing tree-like structure based on parent
I have an XML Sitemap like this
<?xml version="1.0" encoding="utf-8" ?>
<Menu>
<MenuItem Name="Page 1" Url="~/page1.aspx">
<MenuItem Name="SubPage 1" Url="~/subpage1.aspx" />
</MenuItem>
<MenuItem Name="Page 2" Url="~/page2.aspx">
<MenuItem Name="SubPage 1" Url="~/subpage1.aspx" />
<MenuItem Name="SubPage 2" Url="~/subpage2.aspx">
<MenuItem Name="ThirdLevel 1" Url="~/thirdlevel1.aspx" />
<MenuItem Name="ThirdLevel 2" Url="~/thirdlevel2.aspx" />
<MenuItem Name="ThirdLevel 3" Url="~/thirdlevel3.aspx" />
</MenuItem>
<MenuItem Name="SubPage 3" Url="~/subpage3.aspx" />
</MenuItem>
<MenuItem Name="Page 3" Url="~/page3.aspx" />
</Menu>
And a recusive function to loop all this out to an <ul>
menu like this:
// ^^^ loaded all into an XmlNodeList
private void CreateMenuItems(HtmlTextWriter writer, XmlNodeList menuitems)
{
writer.RenderBeginTag(HtmlTextWriterTag.Ul);
foreach (XmlNode item in menuitems)
{
writer.RenderBeginTag(HtmlTextWriterTag.Li);
writer.AddAttribute(HtmlTextWriterAttribute.Href, this.ResolveUrl(item.Attributes["Url"].Value));
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.Write(item.Attributes["Name"].Value);
writer.RenderEndTag();
if (item.HasChildNodes)
{
// Recusive funktion
CreateMenuItems(writer, item.ChildNodes);
}
writer.RenderEndTag();
}
writer.RenderEndTag();
}
All this works very fine and it lists all in a tree-menu like structure.
This is good when I want to create an DropDown menu. But since I want the subpages in a sidebar I need to make a new web control that lists the current page structure based on the upper parent.
So if I am on "Page 2" I want an control to get the items in "Page 2"-ChildNodes. And if I click the "SubPage 2" I want it to list all from the upper parent "Page 2" like:
Page 1
Page 2 *click*
------开发者_StackOverflow社区------ SUB MENU I WANT TO LIST -------------
--> SubPage 1
--> SubPage 2 *click*
--> ThirdLevel 1
--> ThirdLevel 2
--> Third Level 3 *current page*
--> SubPage 3
------------ SUB MENU I WANT TO LIST -------------
Page 3
I am aware I need to detect the current url, finding the XmlNode and go back. But I am unsure how to navigate on that level. :-/
Have you considered using the built in sitemap functionality? FindSiteMapNodeFromKey
No sense handling a bunch of xml if you don't have to.
精彩评论