[Reprinted] Treeview and Recursion

Source: Internet
Author: User
Background: we have encountered this situation today:
You need to bind part of sitemap content to a Treeview control. The sitemap structure is as follows: <? XML version = "1.0" encoding = "UTF-8" ?>
< Sitemap Xmlns = "Http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
< Sitemapnode URL = "~ /Default. aspx" Title = "Homepage" >
< Sitemapnode URL = "~ /Addreport. aspx" Title = "Video error" > </ Sitemapnode >

< Sitemapnode URL = "~ /Recommand. aspx" Title = "Movie recommendation" > </ Sitemapnode >
< Sitemapnode URL = "" Title = "Starstar movie background" >
< Sitemapnode URL = "" Title = "System functions" Description = "Starstar movie Management System" >
< Sitemapnode URL = "~ /Manager/default. aspx" Roles = "Administrators" Title = "Starstar movie background" > </ Sitemapnode >
< Sitemapnode URL = "~ /Manager/noticemanager. aspx" Title = "Announcement Management" > </ Sitemapnode >
< Sitemapnode URL = "" Title = "Administrator management" >
< Sitemapnode URL = "~ /Manager/addmanager. aspx" Title = "Add an administrator" > </ Sitemapnode >
< Sitemapnode URL = "~ /Manager/managers. aspx" Title = "Administrator" > </ Sitemapnode >
</ Sitemapnode >

</ Sitemapnode >
</ Sitemapnode >
</ Sitemapnode >
</ Sitemap >

For example, I want to bind the content starting from the node with the title "Start star movie background" (hereinafter referred to as the target node) to the Treeview, but do not want to bind the node above it. There is a sitemap. currentnode that can get the current node. Therefore, the problem is converted to locating the target node based on the current node. Finally, a recursive function is used to solve this problem: Private Sitemapnode getbackrootnode (sitemapnode currentnode, String Title)
{
If (Currentnode. Title = Title) Return Currentnode; // If yes, this node is returned.
If (Currentnode = Sitemap. rootnode) Return Currentnode; // If it is the first node, the first node is returned. An exception that does not exist can also be thrown here.
Return Getbackrootnode (currentnode. parentnode, title ); // Find the parent node.
}

A tree is a very special structure. Many of its relationships can be expressed by parent or children. Therefore, many recursive solutions are formed. When encountering tree-related content, consider using recursion. The problem may be easily converted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.