Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
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 4.0 Tutorials V4
Server Intellect Cloud Hosting

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.

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>

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

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>

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

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;
}
}

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

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks