Set item is selected in ASP.NET control

ASP.NET newbie here. When on a page I would like to select the appropriate menu item. My approach is this: In Home.aspx.cs:

Menu menu = (Menu)Master.FindControl("Menu1");

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

      

Problem: menu.item.count == 0

. My menu is tied to a sitemap, if that matters.

+2


a source to share


3 answers


I think you should set the selected item in the 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 a sitemap as a data source ...

To create a menu link created from web.sitemap open in a new window ...

On the asp.net page add the OnMenuItemDataBound event:

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

      



In web.sitemap add? character to url:

In the code, remember the MenuItemDataBound event:

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

      

Any url in web.sitemap that contains? will open in a new window. Note that instead of the? if necessary.

ASP.NET Menu Control Overview

+4


a source


OK, that's what I ended up with. (Thanks Leniel for pointing me in the right direction.) The following code is in the code in the main file that contains the menu control. The comments in the code show my surprise that this control does not allow multiple items to be selected at the same time. Thus, in a layered menu, I could not display both the currently selected node and its parent. (If I'm missing something.)



// This is where we set the current node ROOT menu item selected property to
    // true.  The current node won't show as selected, but it 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 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 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 parent node and set it 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 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 not the case.
                //path = parent + "/" + current;
                //MenuItem item2 = menu.FindItem(path);
                //item2.Selected = true;
            }
        }
    }

      

0


a source


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

This way, you don't need to type URL with extra characters when the characters you want (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 to open in a new window, just change the URL from the local link to the full URL (it will take a little longer to download this file because the URL will have to be resolved).

0


a source







All Articles