我想构建动态站点地图,突出显示父菜单,后面的代码包括2个方法:Page_Load和smSource_OnMenuItemDataBound,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
Sessions.SiteMap = Data.GetSiteMapFile();
var xmlSmProvider = new XmlSiteMapProvider();
var providerAttributes = new NameValueCollection();
providerAttributes.Add("siteMapFile", Sessions.SiteMap);
xmlSmProvider.Initialize("RoleDemo", providerAttributes);
xmlSmProvider.BuildSiteMap();
smSource.Provider = xmlSmProvider;
horMenu.DataSource = smSource;
horMenu.DataBind();
}
catch (Exception ex)
{
Data.WriteLog(ex);
}
}
}
protected void smSource_OnMenuItemDataBound(object sender, MenuEventArgs e)
{
try
{
if (SiteMap.CurrentNode != null)
{
if (e.Item.Text == SiteMap.CurrentNode.Title)
{
if (e.Item.Parent != null)
{
e.Item.Parent.Selected = true;
}
else
{
e.Item.Selected = true;
}
}
}
}
catch (Exception ex)
{
Data.WriteLog(ex);
}
}
我的aspx代码文件是这样的:
<asp:SiteMapDataSource runat="server" ID="smSource" ShowStartingNode="False"/>
<asp:Menu runat="server" ID="horMenu" Orientation="Horizontal" OnMenuItemDataBound="smSource_OnMenuItemDataBound">
<LevelMenuItemStyles>
<asp:MenuItemStyle CssClass="main_menu" />
<asp:MenuItemStyle CssClass="level_menu" />
</LevelMenuItemStyles>
</asp:Menu>
&#13;
问题是 SiteMap.CurrentNode 始终返回NULL,因此突出显示菜单无法正常工作。