ASP. net2.0 menu control set static item selected when select its child dynamic items

Source: Internet
Author: User

 

The Menu Control compares the current URL with the URL in its own items, and places the same item in the selected state. However, we usually only display a level-1 static item. When a dynamic item is selected, we cannot directly see it. It can only be seen when the static menuitem drop-down list is opened. And sometimes we do not display dynamic items at all. The general requirement is: as long as the page is organized in a tree structure in the sitemap file, I want to access it, the corresponding root node of the menu control is selected. In this way, with selectedstyle, we can easily distinguish different functional modules of the website from each other using different styles.

Unfortunately, the menu control does not support this function by default. The next version is said to be added. Currently, we can control the menuitemdatabound event as follows:

If (E. Item. Selected & E. Item. parent! = NULL)
{
E. Item. Parent. Selected = true;
}

In this case, you must display all dynamic items, that is, the maximumdynamicdisplaylevels attribute must be greater than 0. However, if you do not want to display dynamic items, you only need to dynamically remove the subitems after completing the above judgment:

If (E. Item. parent! = NULL)
{
E. Item. Parent. childitems. Remove (E. item );
}

The complete code is as follows:

Protected void menu1_menuitemdatabound (Object sender, menueventargs E)
{
If (E. Item. Selected & E. Item. parent! = NULL)
{
E. Item. Parent. Selected = true;
}

If (E. Item. parent! = NULL)
{
E. Item. Parent. childitems. Remove (E. item );
}
}

 

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.