开发者

Set item.selected in ASP.NET Menu Control

ASP.NET newbie here. When on a page I'd like to set the corresponding menu item to selected. My approach is this: On Home.aspx.cs:

Menu menu = (Menu)Master.FindControl("Menu1开发者_Go百科");

if (menu.Items.Count > 0)
{
    menu.FindItem("Home").Selected = true;
}

Trouble is, menu.item.count == 0 . My menu is bound to a sitemap, if that matters.


I think you must set the selected item on MenuItemDataBound event (adapt your code):

protected void Menu1_MenuItemDataBound(object sender, MenuEventArgs e)
{
    if (SiteMap.CurrentNode != null)
    {
        if (e.Item.Text == SiteMap.CurrentNode.Title)
        {
            e.Item.Selected = true;
        }
    }
}

More content that shows how to handle links in a menu that has as datasource a sitemap...

To have a menu link built from web.sitemap open in new window...

In asp.net page add OnMenuItemDataBound event:

<asp:Menu ID="mnuFooter" runat="server"
DataSourceID="SiteMapDataSource1"
OnMenuItemDataBound="mnuFooter_MenuItemDataBound">
</asp:Menu>

In web.sitemap, add a ? character to the url:

In code behind, capture the MenuItemDataBound event:

protected void mnuFooter_MenuItemDataBound(Object sender, MenuEventArgs e)
{
    if (e.Item.NavigateUrl.Contains("?"))
    {
        e.Item.Target = "_blank";
    }
}

Any url in the web.sitemap that contains a ? will open in a new window. Note, use any other valid url character in place of the ? if necessary.

ASP.NET Menu Control Overview


OK, here's what I ended up with. (Thanks, Leniel, for pointing me in the right direction.) The following code is in the code behind of the master file that contains the menu control. Comments in the code reveal my surprise that this control doesn't allow multiple items to be selected at one time. So, in a multi-level menu, I couldn't show as selected both the current node and it's parent. (Unless I'm missing something.)

// This is where we set the current node's ROOT menu item's selected property to
    // true.  The current node won't show as selected, but it's parent will be.
    // As explained elsewhere, this was a UI decision based on the fact that the menu 
    // can only have one menu item selected at a time.
    // If the menu wasn't dataBound, this code would be in Page_Load.
    protected void SideMenu_DataBound1(object sender, EventArgs e)
    {           
        Menu menu = (Menu)this.FindControl("SideMenu");    // Get Menu Object

        string parent = null;

        // First check that there's a current node - there wont be one if the user
        // accesses this page from a bookmark.
        if (SiteMap.CurrentNode != null)
        {
            // Check if the current place in the SiteMap is in the first level of the menu
            // or a child menu item. 
            if (SiteMap.CurrentNode.ParentNode.ParentNode != null)
                parent = SiteMap.CurrentNode.ParentNode.Description;  // It's a child - has a parent

            if (parent == null)  // a parent node
            {
                MenuItem item = menu.FindItem(SiteMap.CurrentNode.Description);
                item.Selected = true;
            }
            else   // a child menu item
            {
                // Get it's parent node and set it's selected property to true.
                MenuItem item = menu.FindItem(parent);
                item.Selected = true;
                // Following comments left in to show how to get at
                // a menu item that's not in the first level.  The trick is the
                // "/" separator, which is the pathSeparator property of the menu
                // control.

                //  The menu control
                // only allows one item to be selected at a time.  This is true, even though
                // the MenuItem class has a selected property, which implies each item 
                // can have a selected property but it's not the case.
                //path = parent + "/" + current;
                //MenuItem item2 = menu.FindItem(path);
                //item2.Selected = true;
            }
        }
    }


If your web.sitemap file contains external links (links to other domains), then those links will need to start with http:// or https://, but your local file references would not - they would just be relative references from the current location of the loaded document.

As such, there's no need to pad your URL's with extra characters, when the characters you need are already there (http or https).

protected void mnuFooter_MenuItemDataBound(Object sender, MenuEventArgs e) 
{ 
    string theURL = e.Item.NavigateUrl;
    if (theURL.Contains("http://") || theURL.Contains("https://")) 
    { 
        e.Item.Target = "_blank"; 
    } 
} 

If you have a local file that you want opened in a new window, just change the URL from a local reference to the full URL (it will take that file slightly longer to load because the URL will have to resolve).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜