[英]how to access site master navigation menues' value to any aspx page in asp.net
I'm developing a website using default "Site.master" master page. 我正在使用默认的“ Site.master”母版页开发网站。 I want to access the value of menu item available at navigation menu in Site.master. 我想访问Site.master中导航菜单上可用的菜单项的值。 For example "Home" and "About" navigation menu item's value. 例如,“主页”和“关于”导航菜单项的值。 I'm new & need some help. 我是新来的,需要帮助。
You need to cast. 你需要演员。 In your page based on a master page, do this: 在基于母版页的页面中,执行以下操作:
var pro = (this.Master as YourMasterPageClassName).SomeInternalToMasterProperty;
Menu mn = (Menu)Master.FindControl("id_of_menu_control");
foreach (MenuItem item in mn.Items)
{
string navigate = item.NavigateUrl;
/// Other stuffs you want to do
}
This is the way that I have done it... 这就是我做到的方式...
Control allUsers = this.Page.Master.FindControl("login").FindControl("loginview").FindControl("ulmenu").FindControl("allUsers");
The above is use on my page load if an "Admin" logs in. Then this is what is in my site.master... 如果“管理员”登录,以上内容将用于我的页面加载。这就是我的site.master中的内容。
<section runat="server" id="login">
<asp:LoginView id="loginview" runat="server" ViewStateMode="Enabled">
<LoggedInTemplate>
<p id="paragraph">
Welcome,
<asp:LoginName ID="loginName" runat="server" CssClass="username" />
</a>!
<asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/Account/Login.aspx" />
<ul runat="server" id="ulmenu" class="menu">
<li><a runat="server" href="~/Dashboard.aspx">Dashboard</a></li>
<li><a runat="server" href="~/DeliveredDeals.aspx">Delivered Deals</a></li>
<li><a runat="server" href="~/DealTracking.aspx">Deal Tracking</a></li>
<li><a runat="server" id="allUsers" href="~/Account/Garage.aspx" visible="false">Garage</a></li>
</ul>
</p>
</LoggedInTemplate>
</asp:LoginView>
</section>
If you check my last li, you want to give it an ID so you are able to find it. 如果您查看我的最后一个li,您想给它一个ID,以便能够找到它。 I hope this helps! 我希望这有帮助!
You can do it as simple as follows: 您可以按照以下步骤进行操作:
switch (e.Item.Value)
{
case "1":
//code
return;
case "2":
//code
return;
case "3":
//code
return;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.