如何让 asp:Menu 跟随 Web.sitemap 中提供的 url?
我有一个简单的 asp:menu-item
它使用 Web.sitemap
来获取菜单项。 页面将回发,但无法获取与单击的项目关联的页面。 我要提到的是,导航栏代码位于母版页文件内。
<div>
<asp:SiteMapDataSource ID="SiteMapDataSource1" ShowStartingNode="false" runat="server" />
<asp:Menu ID="Menu1" Orientation="horizontal" runat="server" BackColor="#a0a080" DataSourceID="SiteMapDataSource1"
DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#a00000"
StaticSubMenuIndent="10px" Style="z-index: 2; left: 390px; position: absolute;
top: 281px" Height="20px" Width="311px">
<StaticSelectedStyle BackColor="#a0a080" />
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DynamicHoverStyle BackColor="#a0a080" ForeColor="White" />
<DynamicMenuStyle BackColor="#a0a080" />
<DynamicSelectedStyle BackColor="#a0a080" />
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DataBindings>
<asp:MenuItemBinding DataMember="SiteMapNode" EnabledField="Title" TextField="Title" />
</DataBindings>
<StaticHoverStyle BackColor="#666666" ForeColor="White" />
</asp:Menu>
</div>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="" title="" description="">
<siteMapNode title="Home" description="Zombie (be)Warehouse" url="index.aspx" />
<siteMapNode title="Armor" description="Anti-Zombie Armor" url="Armor.aspx" />
<siteMapNode title="Weapons" description="Anti-Zombie Weapons" url="Weapons.aspx" />
<siteMapNode title="Manuals" description="Survival Manuals" url="Manuals.aspx" />
<siteMapNode title="Sustenance" description="Prepared food for survival" url="Sustenance.aspx" />
<siteMapNode title="Contacts" description="Contact Us" url="Contacts.aspx" />
<siteMapNode title="About" description="About Zombie (be)Warehouse" url="About.aspx" />
</siteMapNode>
</siteMap>
更新: 问题出现在菜单项的 DataBindings 部分。 请注意这一行:
<asp:MenuItemBinding DataMember="SiteMapNode" EnabledField="Title" Text="Title" />
Text="Title"
设置 Web.sitemap
文本字段中菜单的显示文本。 我注意到 MenuItemBinding
项有一个名为 NavigateUrlField
的字段。
因此,要解决此问题,您只需更改/添加到 asp:MenuItemBinding
<asp:MenuItemBinding DataMember="SiteMapNode" NavigateUrlField="url" EnabledField="Title" TextField="Title" />
I have a simple asp:menu-item
that uses the Web.sitemap
to get the menu items. The page will postback but fails to get the page associated to the clicked item. I will mention that the navigation bar code is within the masterpage file.
<div>
<asp:SiteMapDataSource ID="SiteMapDataSource1" ShowStartingNode="false" runat="server" />
<asp:Menu ID="Menu1" Orientation="horizontal" runat="server" BackColor="#a0a080" DataSourceID="SiteMapDataSource1"
DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#a00000"
StaticSubMenuIndent="10px" Style="z-index: 2; left: 390px; position: absolute;
top: 281px" Height="20px" Width="311px">
<StaticSelectedStyle BackColor="#a0a080" />
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DynamicHoverStyle BackColor="#a0a080" ForeColor="White" />
<DynamicMenuStyle BackColor="#a0a080" />
<DynamicSelectedStyle BackColor="#a0a080" />
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DataBindings>
<asp:MenuItemBinding DataMember="SiteMapNode" EnabledField="Title" TextField="Title" />
</DataBindings>
<StaticHoverStyle BackColor="#666666" ForeColor="White" />
</asp:Menu>
</div>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="" title="" description="">
<siteMapNode title="Home" description="Zombie (be)Warehouse" url="index.aspx" />
<siteMapNode title="Armor" description="Anti-Zombie Armor" url="Armor.aspx" />
<siteMapNode title="Weapons" description="Anti-Zombie Weapons" url="Weapons.aspx" />
<siteMapNode title="Manuals" description="Survival Manuals" url="Manuals.aspx" />
<siteMapNode title="Sustenance" description="Prepared food for survival" url="Sustenance.aspx" />
<siteMapNode title="Contacts" description="Contact Us" url="Contacts.aspx" />
<siteMapNode title="About" description="About Zombie (be)Warehouse" url="About.aspx" />
</siteMapNode>
</siteMap>
Update:
The problem is being found in the DataBindings
section of the menu item. Notice the line:
<asp:MenuItemBinding DataMember="SiteMapNode" EnabledField="Title" Text="Title" />
The Text="Title"
sets the menu's displayed text from the Web.sitemap
's text field. I noticed that the MenuItemBinding
item had a field called NavigateUrlField
.
So to solve this issue, you simple need to change/add to the asp:MenuItemBinding
<asp:MenuItemBinding DataMember="SiteMapNode" NavigateUrlField="url" EnabledField="Title" TextField="Title" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
NavigateUrlField
字段添加到MenuItemBinding
中,如下所示You need to add the
NavigateUrlField
field to theMenuItemBinding
like this<asp:MenuItemBinding DataMember="SiteMapNode" EnabledField="Title" TextField="Title" NavigateUrlField="url"/>