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