Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Website Navigation - Controlling Menus Programmatically in ASP.NET and C#
Controlling Menus Programmatically in ASP.NET and C#


ASP.NET Website Navigation Tutorial

This tutorial shows how we can use two Menu controls to display the SiteMap in a hierarchical structure. The second menu's display will depend upon the selection of the first menu. C# version.


Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

We need to create a Web.sitemap:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
<siteMapNode title="Home">
<siteMapNode title="Products">
<siteMapNode title="Hardware" url="Default.aspx?node=hardware">
<siteMapNode title="Monitors"/>
<siteMapNode title="Speakers"/>
<siteMapNode title="Input Devices"/>
<siteMapNode title="Printers"/>
<siteMapNode title="Hard Drives"/>
</siteMapNode>
<siteMapNode title="Software" url="Default.aspx?node=software">
<siteMapNode title="Operating Systems"/>
<siteMapNode title="Email"/>
<siteMapNode title="Internet"/>
<siteMapNode title="Word Processor"/>
<siteMapNode title="Database"/>
</siteMapNode>
<siteMapNode title="How-Tos" url="Default.aspx?node=howtos">
<siteMapNode title="How To Program"/>
<siteMapNode title="How To Debug"/>
<siteMapNode title="How To Test"/>
</siteMapNode>
</siteMapNode>
<siteMapNode title="Services">
<siteMapNode title="Consultation" url="Default.aspx?node=consultation">
<siteMapNode title="Processes"/>
<siteMapNode title="Management"/>
<siteMapNode title="Recruiting"/>
</siteMapNode>
<siteMapNode title="Development" url="Default.aspx?node=development">
<siteMapNode title="Web Apps"/>
<siteMapNode title="Enterprise Apps"/>
<siteMapNode title="Database"/>
</siteMapNode>
</siteMapNode>
<siteMapNode title="Support">
<siteMapNode title="Downloads" url="Default.aspx?node=downloads">
<siteMapNode title="Audio Drivers"/>
<siteMapNode title="Network Adapter Drivers"/>
<siteMapNode title="Printer Drivers"/>
<siteMapNode title="Graphics Drivers"/>
</siteMapNode>
<siteMapNode title="Manuals" url="Default.aspx?node=manuals">
<siteMapNode title="Applications"/>
<siteMapNode title="Troubleshooting"/>
<siteMapNode title="Installation"/>
<siteMapNode title="Internet"/>
</siteMapNode>
</siteMapNode>
</siteMapNode>
</siteMap>

There are two SiteMapDataSources; one for the first menu, and one for the second menu. The second SiteMapDataSource has the StartingNodeUrl set to
Default.aspx?node=hardware
The ASPX code:

<form id="form1" runat="server">
<div>
<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1" MaximumDynamicDisplayLevels="0" OnMenuItemClick="Menu1_MenuItemClick" Orientation="Horizontal">
<DataBindings>
<asp:MenuItemBinding DataMember="SiteMapNode" TextField="Title" />
</DataBindings>
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="False" />
<br />
<asp:Menu ID="Menu2" runat="server" DataSourceID="SiteMapDataSource2" Orientation="Horizontal">
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource2" runat="server" ShowStartingNode="False" StartingNodeOffset="-1" StartingNodeUrl="Default.aspx?node=hardware" />
<br />
<br />
</div>
</form>

The C# code for the Click event for the first menu should look something like this:

protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{
switch(e.Item.Value)
{
case "Products":
SiteMapDataSource2.StartingNodeUrl = "Default.aspx?node=hardware";
return;
case "Services":
SiteMapDataSource2.StartingNodeUrl = "Default.aspx?node=consultation";
return;
case "Support":
SiteMapDataSource2.StartingNodeUrl = "Default.aspx?node=downloads";
return;
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks