Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ASP.NET菜单控件中设置item.selected_Asp.net_Menu_Controls - Fatal编程技术网

在ASP.NET菜单控件中设置item.selected

在ASP.NET菜单控件中设置item.selected,asp.net,menu,controls,Asp.net,Menu,Controls,这里是ASP.NET新手。在页面上时,我想将相应的菜单项设置为选中。我的做法是: 在Home.aspx.cs上: Menu menu = (Menu)Master.FindControl("Menu1"); if (menu.Items.Count > 0) { menu.FindItem("Home").Selected = true; } 问题是,menu.item.count==0。 “我的菜单”已绑定到网站地图(如果需要)。我认为您必须在MenuItemDataBoun

这里是ASP.NET新手。在页面上时,我想将相应的菜单项设置为选中。我的做法是: 在Home.aspx.cs上:

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

if (menu.Items.Count > 0)
{
    menu.FindItem("Home").Selected = true;
}
问题是,
menu.item.count==0

“我的菜单”已绑定到网站地图(如果需要)。

我认为您必须在MenuItemDataBound事件中设置所选项目(调整代码):

更多内容显示了如何处理作为数据源的菜单中的链接站点地图

要在新窗口中打开从web.sitemap生成的菜单链接

在asp.net页面中添加OnMenuItemDataBound事件:

<asp:Menu ID="mnuFooter" runat="server"
DataSourceID="SiteMapDataSource1"
OnMenuItemDataBound="mnuFooter_MenuItemDataBound">
</asp:Menu>
protected void mnuFooter_MenuItemDataBound(Object sender, MenuEventArgs e)
{
    if (e.Item.NavigateUrl.Contains("?"))
    {
        e.Item.Target = "_blank";
    }
}
web.sitemap中是否有包含的url?将在新窗口中打开。注意,请使用任何其他有效的url字符代替?如果必要的话


好的,这就是我的结局。(谢谢你,莱尼尔,为我指明了正确的方向。) 以下代码位于包含菜单控件的主文件的代码隐藏中。 代码中的注释显示了我的惊讶,这个控件不允许同时选择多个项目。因此,在多级菜单中,我无法将当前节点及其父节点显示为选中状态。(除非我遗漏了什么。)


如果您的web.sitemap文件包含外部链接(指向其他域的链接),则这些链接需要以http://或https://开头,但您的本地文件引用不会,它们只是来自加载文档当前位置的相对引用

因此,当您需要的字符已经存在(http或https)时,不需要在URL中添加额外的字符


如果要在新窗口中打开本地文件,只需将URL从本地引用更改为完整URL(加载该文件需要稍长时间,因为URL需要解析)。

您使用哪种方法调用上面显示的代码?也许你是在填充菜单之前调用它。代码在Home.aspx的页面中。可能是因为菜单绑定到sitemapdatasource,所以它尚未填充。我对此感到奇怪。人们一定有这个工作,但我什么也找不到。我将尝试在没有数据源的情况下设置项目-看看这会如何影响事情。是的,我在设计器中设置了项目,它们在页面加载中。所以我猜如果菜单是绑定的,你就不能这么做?可能是站点地图对象中的某些内容。。。(这些控件不仅仅是学习javascript的工作。)灯泡。就像Leniel所说的-主机中的数据绑定事件可能会这样做。谢谢。我有一个后续问题:如果我不使用网站地图,而是使用一个普通的Asp.net:菜单-我如何为所选问题设置菜单项的样式?我希望在用户单击某个菜单项后保存样式(以创建伪选项卡控件)
protected void mnuFooter_MenuItemDataBound(Object sender, MenuEventArgs e)
{
    if (e.Item.NavigateUrl.Contains("?"))
    {
        e.Item.Target = "_blank";
    }
}
// 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;
            }
        }
    }
protected void mnuFooter_MenuItemDataBound(Object sender, MenuEventArgs e) 
{ 
    string theURL = e.Item.NavigateUrl;
    if (theURL.Contains("http://") || theURL.Contains("https://")) 
    { 
        e.Item.Target = "_blank"; 
    } 
}