Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET navigation


May 12, 2021 ASP.NET


Table of contents


ASP.NET Web Forms - Navigation

This section focuses on how ASP.NET and navigation controls are used.

ASP.NET with built-in navigation controls.


Site navigation

Maintaining menus on large websites is difficult and time-consuming.

In ASP.NET, menus can be stored in files, making them easy to maintain. The file is usually called web.sitemap and is stored at the root of the Web site.

In addition, ASP.NET has three heart navigation controls:

  • Dynamic menus
  • TreeViews
  • Site Map Path

Sitemap file

For this tutorial, use the following sitemap file:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<siteMap>
<siteMapNode title="Home" url="/aspnet/w3home.aspx">
<siteMapNode title="Services" url="/aspnet/w3services.aspx">
<siteMapNode title="Training" url="/aspnet/w3training.aspx"/>
<siteMapNode title="Support" url="/aspnet/w3support.aspx"/>
</siteMapNode>
</siteMapNode>
</siteMap>

Rules for creating sitemap files:

  • The XML file must contain the hashtags around the content
  • The label can only have one child node ("home" page)
  • Each slt;siteMapNode can have multiple child nodes (web pages)
  • Each has properties that define the page title and URL

ASP.NET navigation Note: The sitemap file must be under the site root, and the URL property must be relative to the root.


Dynamic menu

The control displays a standard site navigation menu.

Examples of code:

<asp:SiteMapDataSource id="nav1" runat="server" />

<form runat="server">
<asp:Menu runat="server" DataSourceId="nav1" />
</form>

The control in the example above, slt;asp:Menu, is a placeholder for the server to create navigation menus.

The control's data source is defined by the DataSourceId property. id"nav1" connects the data source to the controls of the .lt;asp:SiteMapDataSource.

The SiteMapDataSource control automatically connects to the default sitemap file (web.sitemap).


Treeview

The control displays a multi-level navigation menu.

This menu looks like a tree with branches and leaves, which can be opened or closed by the sign .

Examples of code:

<asp:SiteMapDataSource id="nav1" runat="server" />

<form runat="server">
<asp:TreeView runat="server" DataSourceId="nav1" />
</form>

The control in the example above, slt;asp:TreeView, is a placeholder for the server to create navigation menus.

The control's data source is defined by the DataSourceId property. id"nav1" connects the data source to the controls of the .lt;asp:SiteMapDataSource.

The SiteMapDataSource control automatically connects to the default sitemap file (web.sitemap).


SiteMapPath

The SiteMapPath control displays a pointer (navigation path) to the current page. The path appears as a clickable link to the parent page.

Unlike treeView and Menu controls, SiteMapPath controls do not use SiteMapDataSource. The SiteMapPath control uses the web.sitemap file by default.

ASP.NET navigation Tip: If SiteMapPath doesn't display correctly, it's most likely due to a URL error (print error) in the web.sitemap file.

Examples of code:

<form runat="server">
<asp:SiteMapPath runat="server" />
</form>

The control in the example above, the siteMapPath, is a placeholder for the server to create navigation menus.

The above is about ASP.NET navigation, through the website navigation, you can quickly access a page.