Create a main menu with EPiServer:MenuList
Describes how to create a basic menu list of top level pages using the EPiServer:MenuList control.
Important
This topic applies to Web Forms.
A top-level navigational menu enables the user to navigate around the website.
Create a main menu with EPiServer:MenuList
as follows:
- Add some pages under the website's start page in edit view, such as News, Documents, Examples, and Events. You can go to and from these pages when the menu is complete.
- In
web.config
, set the variablepageStartId
to the website's start page ID. In the menu list, display each page's name as a link to that page; use the built-inPageLink
property of the current page. - Add an Optimizely Content Management System (CMS) web control to the project;
MainMenu
in this example. - In the
MainMenu.ascx
user control file, add the control with closing tags and give the control the ID value Menu. - The
PageLink
attribute is set in the control's code-behind file. Set the PageLink attribute and the control ID, and see the code example in this topic. - Add an item template
<ItemTemplate>
around the CMSPageLink
property. These recur for every page item that exists in the list. The start page in the page hierarchy displays a page link for the pages. - Pass in the correct CSS class to get the right look and feel (
CssClass="NormalLink"
), by setting theCssClass
attribute on theProperty
control. - The
PageLink
attribute of theEPiServer:MenuList
control should be made dynamic instead of using a hardcoded ID to the website's start page. A reference to the start page of the website is automatically available through the staticStartPage
property in the EPiServer.Core.PageReference class, which provides context to the menu control, indicating the pages it will display. In the code-behind file, set the control attributeMenu.PageLink
to thePageReference.StartPage
property. See the code example in this topic. - Use data binding to access the property and use the retrieved information as part of the loading of a control. (Data binding is a .NET concept documented in the Microsoft .NET help files.) You must explicitly data vind the menu list control by calling its
DataBind()
method, by placing the example code (in this topic) in thePage_Load
event in the code-behind file (where menu is the ID of yourMenuList
 web control).
The menu should be functional now. - Browse to the website's start page and verify that the top menu accurately displays the names of the pages at the top level of the website structure. Click the links in the menu to verify that the navigation is working.
MenuList code example markup
<EPiServer:MenuList runat="server" id="Menu">
<HeaderTemplate>
<ul id="MainMenu">
</HeaderTemplate>
<ItemTemplate>
<li class="unselected"><EPiServer:Property PropertyName="PageLink" runat="server" /></li>
</ItemTemplate>
<SelectedTemplate>
<li class="selected"><EPiServer:Property runat="server" PropertyName="PageLink" /></li>
</SelectedTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</EPiServer:MenuList>
MenuList code example code behind
public partial class MainMenu: UserControlBase {
protected override void OnLoad(System.EventArgs e) {
base.OnLoad(e);
Menu.PageLink = PageReference.StartPage;
Menu.DataBind();
}
/// <summary>
/// Gets or sets the MenuList for this control
/// </summary>
public MenuList MenuList {
get {
return Menu;
}
set {
Menu = value;
}
}
}
Updated 7 months ago