Using Menus in an ASP.NET Web ApplicationBy Scott Mitchell
Imagine you had just installed a new program to help prepare your taxes and, not wanting to bother with reading the instructions or help files, needed to figure out how to start entering the needed information. What would you do to get started? You'd likely scan the user interface, looking for a toolbar or menu. If the software program you installed was well-designed, it should be relatively easy to find out how to get started. The reason many computer programs are easy is due to the homogeneity of their user interfaces. We're accustomed to programs having a menu and a toolbar. We know that there will, at minimum, be menu items named File, Edit, and Help.
Not unlike desktop applications, Web applications also have standard user interface components. Examples of such standard user interface designs for Web sites include:
- The name or logo of the Web site at the top-center or top-left of each and every Web page that, when clicked, returns the user to the homepage.
- A listing of the site's features or sections along the top and/or the left- or right-hand side.
- A reiteration of the site's features or sections at the bottom-center of each Web page.
There are, of course, others, but these three design rules hold for most every major Web site. (Granted, some Web sites purposefully break these "rules" in an attempt to look artsy, edgy, modern, or cutting-edge.)
When designing a Web site it's typically not a good idea to break from the standard user interface design principles employed by the mass of other Web sites. However, it is usually safe to add new user interface design themes into a Web site application if the design theme is one your users are likely to already be intimately familiar with. Since menus are commonplace in desktop applications, they can fit well into a Web site as well.
Web Site Menu Basics
We are all, no doubt, quite familiar with the purpose of menus and the user experience they present. A user moves his mouse cursor over a menu item and clicks his mouse, thereby showing the menu's items. Clicking a menu item might cause some action to occur, or might display another "sub" menu of additional, related choices. Web site menus typically differ just slightly in their user experience. First, Web site menus usually don't require the user to click to display the menu items. Furthermore, the "action" that is performed when clicking on a menu item oftentimes is taking the user to some URL.
For an example of a Web site menu, check out CrateAndBarrel.com. Along the upper part of the page, there is a menu with top-level menu items: Shop, Gift Registry, Gift Ideas, and Fresh Crate. Moving your mouse over Shop or Gift Registry displays a series of related menu options; for example, under the Gift Registry menu item, there are four choices: Main Page, Give a Gift, Create a Registry, and Edit Your Registry. Clicking on the Give a Gift menu item whisks you to a Web page with a wizard for helping you give a gift.
Another challenge with Web site menus is specifying the data that appears in each menu item. How do the Web page developers at CrateAndBarrel.com specify the contents of the menu, and where clicking a menu item will take the user? Since menu's typically can have an arbitrary amount of nesting - that is, a menu could have several submenus, each of which have menu items with submenus, each of which have menu items with submenus, and so on and so on - Due to this limitless nesting, XML files are often used to specify the structure of a menu. (XML is a good fit because it allows for easily specifying hierarchical, related data; for more information on XML be sure to check out the XML FAQ section on ASPFAQs.com.) The precise elements used in the XML data file would differ based on the menu component used, but CrateAndBarrel.com's menu might use an XML file that looked like:
While XML files work well for static menus, in some situations the menu needs to be dynamically generated. If different users have different levels of access, you might need to display certain menu items for certain users only. Most Web site menu components provide some means for dynamically constructing the menu's structure for these situations.
Creating a robust Web site menu control from scratch can be a daunting and time-consuming task. Fortunately there are numerous ASP.NET menu controls available, which can be easily plugged into an ASP.NET site in a matter of minutes. A lengthy listing of menu controls can be found at the ASP.NET Control Gallery. Many of these controls have online demos so you can see the control in action.
Examining skmMenu - a Free, Open-Source Menu Control
One of the many ASP.NET menu controls available is skmMenu, an open-source ASP.NET menu control that I created back in October 2003 for an MSDN article. Since then, skmMenu has been enhanced by dozens of developers world-wide. Let's examine the current version of skmMenu, skmMenu 2.0. (skmMenu 2.0 is the latest version of skmMenu as of the time of writing, January 28th, 2004.)
The first step to getting started with using skmMenu is to download skmMenu from the official Web site - http://www.datawebcontrols.com/menu. If you are using the .NET Framework version 1.1 and just want to use skmMenu in your ASP.NET application and are not interested in examining, modifying, or enhancing the underlying source code, you can download the skmMenu binaries. If you are using the .NET Framework version 1.0 or want to tinker around with skmMenu's source code, download the complete source code.
If you download the source code, your first step will be to compile the code into an assembly (a
If you are using Visual Studio .NET 2003, this is as simple as opening the skmMenu project and building the solution.
If you are using Visual Studio .NET 2002, you will need to create a new C# Web Control Library project and then manually
add in the files downloaded. (You'll also need to add the
System.Design.dll assembly to the project's
References folder.) Building the solution will yield the assembly file
Once you have the
skmMenu.dll assembly - either by compiling the source code or by downloading the
skmMenu.dll file directly - you are ready to start using it in an ASP.NET Web application. Start by
creating or opening an ASP.NET Web application in Visual Studio .NET. Our first task will be to add skmMenu to the
Visual Studio .NET Toolbox. This is accomplished by right-clicking on the Toolbox and choosing Add/Remove Items...
Doing so will display a dialog box with two tabs: .NET Framework Components and COM Components. Make sure the
.NET Framework Components tab is selected, click the Browse button, navigate to the
file, and click OK. This will add an item called Menu to the Toolbox.
To add skmMenu to an ASP.NET Web page, simply drag and drop the Menu item from the Toolbox onto the ASP.NET page Designer. You can then tweak the appearance of the menu through its properties in the VS.NET Properties pane.
skmMenu provides both a horizontal and vertical layout of the top-level menu items. The screenshot above shows the menu in its horizontal layout. An example of a vertical layout can be seen in this live demo. Furthermore, skmMenu allows for a high degree of customization of its appearance. Text or images can be used, different images or styles can be used in response to various client-side events, such as moving the mouse over a menu item. (For an example of using rollover images with skmMenu, check out this example.)
skmMenu, like most menu components, can have its content specified statically via an XML file, or dynamically. The code for
assigning an XML data source to skmMenu closely resembles the databinding code used with the data Web controls. Specifically,
you set the
DataSource property to the XML file that contains the data, and then call the
method - that's it, just two lines of code!
Numerous examples of skmMenu in action are available on the datawebcontrols.com/menu Web site; there are additional examples available for download as well. There's also extensive online documentation. Best of all, the complete source code is available, so you can make any changes or enhancements to the code base as required.
|For Technical Information on skmMenu|
|Interested in creating your own ASP.NET server controls? Wondering how skmMenu works behind the scenes? I've written two articles on MSDN detailing the internals of skmMenu:|