Monday, 25 July 2011

Controlling Menus Programmatically in ASP.NET and VB


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. VB 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>

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

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>

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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

Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As MenuEventArgs)
Select Case 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
End Select
End Sub

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment