Chapter 4.2 Web Site Navigation
Chapter 4.2 Web Site Navigation
<a href="Home.aspx">Home</a>
ASP.NET Sitemap
ASP.NET Sitemap control allows to programma cally define and display a website's naviga on
structure in a hierarchical format. It is commonly used to generate a site map or naviga on
menu based on the structure of the website. In order to use Sitemap control in website follow
following steps.
1. Create a new XML file and name it "SiteMap.xml". The structure of the xml file will be as
below:
Author: Sharad Pyakurel
2. Add the Sitemap control to the desired loca on of the web page, such as the master
page or any specific content page.
<div>
<asp:TreeView ID="tvSitemap" runat="server" DataSourceID="DsSiteMap">
</asp:TreeView>
<asp:SiteMapDataSource ID="DsSiteMap" runat="server" />
</div>
- The SiteMapDataSource control is used to bind the sitemap data to the TreeView control.
- The TreeView control is responsible for rendering the hierarchical structure of the site
map.
- The SiteMapFile property of the SiteMapDataSource control is set to the path of the
sitemap file.
- The sitemap structure defined in the XML file will be displayed in the TreeView control.
TreeView Control
TreeView control: The TreeView control is useful for displaying hierarchical data. It can be
populated manually or bound to a data source.
- The ExpandDepth property can be used to control the number of levels ini ally expanded.
- The SelectedNodeChanged event can be used to perform an ac on when a node is
selected.
Menu Control
Menu control in ASP.NET allows you to create hierarchical menus for the web applica on.
Asp.net Menu Control feature provides a consistent way for website naviga on. Menu control
can be added to the web page by simply drag and drop from ToolBox > Naviga on > Menu or
by adding the following few lines of code.
<style type="text/css">
body {
background-color: mediumaquamarine;
font-family: Arial;
font-size: 10pt;
color: #444;
}
.ParentMenu, .ParentMenu:hover {
width: 100px;
background-color: #fff;
color: #333;
text-align: center;
height: 30px;
line-height: 30px;
margin-right: 5px;
}
.ParentMenu:hover {
background-color: #ccc;
}
Author: Sharad Pyakurel
.ChildMenu, .ChildMenu:hover {
width: 110px;
background-color: #fff;
color: #333;
text-align: center;
height: 30px;
line-height: 30px;
margin-top: 5px;
}
.ChildMenu:hover {
background-color: #ccc;
}
.selected, .selected:hover {
background-color: #A6A6A6 !important;
color: #fff;
}
.level2 {
background-color: #fff;
}
</style>
- The above css can be applied to the menu control as following code:
- Menu items also can be populated dynamically based on data in any database table. Let
us define a DataTable where the DataTable contains details of menu items with parent
and children rela onship. Following code creates a DataTable.
dr["ParentId"] = 0;
dr["Title"] = "About";
dr["Descrip on"] = "About us page";
dr["URL"] = "~/AboutUs.aspx";
dt.Rows.Add(dr);
ds.Tables.Add(dt);
var dv = ds.Tables[0].DefaultView;
dv.RowFilter = "ParentId='" + parentmenuId + "'";
DataSet ds1 = new DataSet();
var newdt = dv.ToTable();
return newdt;
}
- The following method gets data for the menu from the method GetMenuData and loads
the menu data in menu control.
parentMenuItem.ChildItems.Add(menuItem);
Author: Sharad Pyakurel
- Finally, the method to load the menu can be called from Page Loan method.