Click here to Skip to main content
11,640,125 members (68,831 online)
Click here to Skip to main content

Very Compatible DHTML Menu ASP.NET User Control

, 10 Apr 2005 309.6K 13.4K 166
Rate this:
Please Sign up or sign in to vote.
An ASP.NET user control for a DHTML drop down menu, with high compatibility and customization features.

Sample Image of very compatible DHTML menu

Sample Image of very compatible DHTML menu

Introduction

A DHTML menu is something many web developers use these days as those provide a friendly and hierarchical interface for users. In a project I’m working on, I need one of those, and I searched the web to find an ASP.NET control. There are some good examples of menu controls but none of them gives what really I want (cross-browser, VB.NET, code available, free, and with possibilities for nice styles). Then I found Sylvain Machefert’s work (in French). He is a web developer with good CSS, JavaScript and cross browser issues knowledge, who made a very compatible DHTML menu and released it to public. The menu even works on text based browsers, reading technologies for visually impaired considered, and the menu is not visible when printing your page. Also, it can be customized in many ways and it supports icon images. The list of compatible browsers:

  • Internet Explorer 5/5.5/6 Windows 98/2000/XP;
  • JAWS Vocal Synthesis in IE6 Windows XP;
  • MyIE2;
  • Netscape 7 Windows;
  • Netscape 6.2;
  • Mozilla 1.4+ Windows 2000/XP;
  • FireFox 0.9+;
  • Opera 7 Windows and Linux;
  • Lynx (text browser)
  • Encompass 0.5 et Dillo 0.8 (non CSS browsers) Linux (Debian);
  • Epiphany 1.2 Linux (Debian);
  • Galéon 1.2;
  • Konqueror Linux (Knoppix);
  • Mozilla Linux (Knoppix);
  • Internet Explorer 5 Mac;
  • Safari 1.0 MacOS X 10.2.8;
  • Safari 1.1 MacOS X 10.3;
  • Camino MacOS X;
  • Firebird MacOS X;
  • OmniWeb MacOS X;
  • Mozilla 1.6 Mac;
  • NeoPlanet 5.1;

Because Sylvain doesn’t have enough knowledge in .NET programming (at the time of this writing at least) he couldn’t release an ASP.NET custom control. There has been a previous example of a user control with Sylvain’s menu, made by guys at Sokhar (a web development company). However, there were a few issues with that one which didn’t go well with my project:

  • They used an old version of Sylvain's code, so less compatibility.
  • Their code is in C#, my project is in VB.NET, because I needed some changes it didn't work out.
  • I thought some improvements could be made besides multi browser issues, such as adding links to main menu titles, a bit improved HTML output, and the ability to change the looks of menu in each page of your project by using the control's properties.
  • Access database idea was great but I needed to implement it to my SQL project.

The only downside for some might be that the menu only supports one level branching. This is no problem for me because I believe more than two levels already messes up the looks of the page (and also I read an article on that matter).

I thank first Sylvain for his great work, and then Sokhar for their contribution; it was an important influence for my code.

Background

This is the first update of this DHTML menu user control. In this release, I made some improvements I was planning after I made the first release.

Well, what does the user control I made do? It reads which CSS file to use from the control property “CSSFile” as defined in your tag definition of the menu control in the aspx page and other properties that tweak menu looks. It loads menu data from an Access file and prepares a nice HTML output which is to be placed in the output of your aspx page. I used Access stored procedures to retrieve some data. I use stored procedures when I work with SQL, and as far as I know, stored procedures are a better way of dealing with data than using strings as command parameters. For those who don’t know about procedures, my code can be a good example work. It was, for me, an educative task as well, because Access (Microsoft Jet Engine, what Access uses as SQL implementation) procedures are defined a bit different than SQL Server ones. I learned about that during this work. My example project is ready for a vertical menu, centered vertically and with gradient filled menu backgrounds. There is also a CSS file included in the first release in the menu folder, so if you want, by changing properties (especially CSSFile property), you can get a different look with the example project. I translated the JavaScript file's comments to ease your way when improving my code or changing menu attributes. Code is heavily commented so I believe you won't have much to miss there. Feel free to ask if you don't understand something.

Using the code

Because I send .vb code pages as well, you can improve this control. Implementation is actually easy. First, create a “databases” folder in your project (right click the project name in your Visual Studio Solution Explorer window, not the solution name, solution name is the one at the top; Add >> New Folder, rename it to “databases”), then right click on the “databases” folder in your Visual Studio Solution Explorer window, Add >> Add Existing Item. You select in the file selection window your Access database file named “cssmenudb.mdb” (or edit the one I’m sending and select that). Be careful with “File Type” in selection window, it must be “All Files” or you won’t see the database file. Now create a folder named “menu” in your project folder (again by right clicking on the project name in the Solution Explorer). Use right click Add >> Add Existing Item method on “menu” folder to add menu.ascx and menu.ascx.vb files from my project (you can select more than one file on that window). You also need to add JavaScript CSS and icon images as well in that folder. Then the menu user control has to be registered in the page you want it to be implemented, by adding the line:

<%@ Register TagPrefix="userCtrl" TagName="CSSMenu" Src="menu/menu.ascx" %>

on top of the aspx page in HTML view, just below the Page directive. Now, you can add your menu’s tag in the HTML view, as in:

<userCtrl:CSSMenu id="Menu1" runat="server" HRImage="lookxphr.gif" 
      FollowScroll="false" CenterMenu="true" DefaultHeight="25" DefaultWidth="95" 
      Vertical="true" DiffWidth="true" CSSFile="vertical.css">
</userCtrl:CSSMenu>

I recommend you to put it above the form tag that is created automatically by Visual Studio in every aspx page. That’s it. You are ready to go.

Let's examine the properties of menu control:

  • HRImage

    If a submenu item has an image file with the same name of the value held in this property, then that menu item is considered as a horizontal ruler (separator). Image is loaded as a separator and tiled horizontally. Nothing more is printed for that menu item. Takes a string for filename of horizontal ruler image.

  • FollowScroll

    This boolean value of "True" or "False" describes whether menu should stay back when page moves forward when scrolled (if page is larger than standard window area) or follow the scroll and be visible all times.

  • CenterMenu

    Boolean value and its name tells all. If menu is in vertical mode (defined by Vertical property) and this property has value "True" then menu is centered vertically, and horizontally if menu is in horizontal mode.

  • Vertical

    Another boolean value. "True" for a vertical menu, "False" for horizontal.

  • DefaultHeight

    An integer number for main menu items' default height in pixels.

  • DefaultWidth

    An integer number for main menu items' default width in pixels.

  • DiffWidth

    The name should not confuse you. This boolean value property defines if main menu items are in different widths according to the number of letters in their title if they are in horizontal mode, or if main menu items are in different heights if they are in vertical mode.

  • CSSFile

    This string valued property keeps the name of the CSS file.

Points of Interest

This is one of the two stored procedures used in the Access database. As you can see (if you are familiar with this concept), the syntax is a bit different from SQL's one.

PROCEDURE sp_mainMenuURL
PARAMETERS mainMenuTitleArg Text ( 255 );
SELECT  tblMainMenu.mainURL
FROM tblMainMenu
WHERE  tblMainMenu.mainMenuTitle = mainMenuTitleArg;

mainMenuTitleArg is the argument this procedure takes to find which main menu item's URL we want to fetch from the database. Once the procedure is created in Access database (in Queries section, we prepare this code), it automatically deletes the first line with PROCEDURE statement.

For those of you unfamiliar with SQL procedures, here is the stored procedure above in TSQL (SQL implementation of Microsoft SQL Server 2000):

PROCEDURE sp_mainMenuURL
CREATE PROCEDURE sp_mainMenuURL
@mainMenuTitleArg NvarChar (255)
AS
SELECT mainURL
FROM tblMainMenu
WHERE mainMenuTitle=@mainMenuTitleArg;
GO

What to do next?

I made many improvements in this release I wanted to after I made version 1.0. Now I think it's time to give the users the facility to have more than one menu in their project. Consider a case where some users (let's say customers) are supposed to get different main menu and submenu items and other users (let's say employees) other items. Of course, in that case, security and more database tables come into consideration.

History

  • Version 1.4
    • Bug fix: Page refresh doesn't invalidate JavaScript and stylesheet anymore which used to happen in some cases before.
    • Everything to be written in HTML is kept in one ASP literal.
    • A constructor is added with default values of properties in case user forgets defining them in tag.
    • Seven new properties added for easier menu customization. No need to modify JavaScript file.
  • Version 1.0
    • First release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

scubaduba
Software Developer
Turkey Turkey
Bogac Guven

I love technology , programming, foreign languages...list goes on. I have a small office in Istanbul and providing software development services.

You may also be interested in...

Comments and Discussions

 
QuestionMenu Position in Master Page Pin
Elie Sawma25-Oct-10 2:22
memberElie Sawma25-Oct-10 2:22 
Questionhi Pin
Murat DOGANCAY14-Oct-09 6:41
memberMurat DOGANCAY14-Oct-09 6:41 
GeneralMy vote of 1 Pin
softer15-Jul-09 9:20
membersofter15-Jul-09 9:20 
QuestionHelp need on customizations Pin
Praveen Srinivas6-Dec-08 12:56
memberPraveen Srinivas6-Dec-08 12:56 
GeneralProblem of Inherits="CSSMenuNew.menu Pin
needhi_p22-Apr-08 2:27
memberneedhi_p22-Apr-08 2:27 
GeneralRe: Problem of Inherits="CSSMenuNew.menu Pin
ravi.g18-Nov-08 21:23
memberravi.g18-Nov-08 21:23 
Questionupdated for sql but needing recompile ? Pin
Voxan Rider4-Nov-07 19:27
memberVoxan Rider4-Nov-07 19:27 
GeneralSource Code Pin
Lu5ck6-Nov-06 16:21
memberLu5ck6-Nov-06 16:21 
Generalplease give me the code for using the menu with sql server database Pin
indrani2215-Oct-06 23:28
memberindrani2215-Oct-06 23:28 
GeneralRe: please give me the code for using the menu with sql server database Pin
Voxan Rider5-Nov-07 4:27
memberVoxan Rider5-Nov-07 4:27 
GeneralMenu Appearance Pin
meerak2-Aug-06 23:31
membermeerak2-Aug-06 23:31 
GeneralRe: Menu Appearance Pin
Imparatorvolkan17-Oct-06 8:36
memberImparatorvolkan17-Oct-06 8:36 
QuestionRe: Menu Appearance Pin
yejivanhai22-Mar-07 7:18
memberyejivanhai22-Mar-07 7:18 
GeneralIE Crashes with smartnav on Pin
dbaser9-May-06 4:35
memberdbaser9-May-06 4:35 
GeneralMenu position Pin
mburagohain27-Feb-06 23:36
membermburagohain27-Feb-06 23:36 
GeneralRemove image boxes from the sub-menus Pin
BryanSherlock30-Jan-06 10:39
memberBryanSherlock30-Jan-06 10:39 
GeneralHorizontal Menu Pin
Richard Tibang14-Dec-05 18:44
memberRichard Tibang14-Dec-05 18:44 
GeneralSub menu problem Pin
Ganesh M10-Nov-05 21:16
memberGanesh M10-Nov-05 21:16 
GeneralMenu appearance problem in Safari on Mac OS Pin
NazM5-Oct-05 20:26
memberNazM5-Oct-05 20:26 
GeneralRe: Menu appearance problem in Safari on Mac OS Pin
praveen_4569820-Sep-07 19:54
memberpraveen_4569820-Sep-07 19:54 
QuestionCould we use in our site? Pin
aminey16-Sep-05 9:57
memberaminey16-Sep-05 9:57 
QuestionVery Nice control but can you change the background color? Pin
dan1808817-Aug-05 8:17
memberdan1808817-Aug-05 8:17 
GeneralAvoid Wrapping around Pin
rfalagan1-Aug-05 5:49
memberrfalagan1-Aug-05 5:49 
GeneralRe: Avoid Wrapping around Pin
scubaduba6-Aug-05 8:29
memberscubaduba6-Aug-05 8:29 
GeneralDrop down List Pin
Micekillerus3-Jun-05 5:04
memberMicekillerus3-Jun-05 5:04 
GeneralRe: Drop down List Pin
scubaduba28-Jun-05 1:25
memberscubaduba28-Jun-05 1:25 
Generalc# version Pin
wisey33@webmail.co.za18-May-05 1:45
memberwisey33@webmail.co.za18-May-05 1:45 
GeneralRe: c# version Pin
scubaduba19-May-05 4:52
memberscubaduba19-May-05 4:52 
GeneralRe: c# version Pin
megoom15-Jun-05 2:19
membermegoom15-Jun-05 2:19 
GeneralRe: c# version Pin
scubaduba28-Jun-05 1:20
memberscubaduba28-Jun-05 1:20 
QuestionAny chance to have an XML implementation? Pin
axplains26-Apr-05 0:27
memberaxplains26-Apr-05 0:27 
AnswerRe: Any chance to have an XML implementation? Pin
scubaduba27-Apr-05 8:54
memberscubaduba27-Apr-05 8:54 
GeneralRe: Any chance to have an XML implementation? Pin
rutgervanhagen14-Jul-05 2:16
memberrutgervanhagen14-Jul-05 2:16 
GeneralRe: Any chance to have an XML implementation? Pin
freesmile13-Jun-07 8:01
memberfreesmile13-Jun-07 8:01 
QuestionWhat about SolPart ASP.NET Menu Pin
tudort21-Apr-05 19:21
susstudort21-Apr-05 19:21 
AnswerRe: What about SolPart ASP.NET Menu Pin
scubaduba22-Apr-05 22:00
memberscubaduba22-Apr-05 22:00 
QuestionNice but how to submenu in submenu? Pin
Tadzesi20-Apr-05 20:43
memberTadzesi20-Apr-05 20:43 
AnswerRe: Nice but how to submenu in submenu? Pin
scubaduba21-Apr-05 2:06
memberscubaduba21-Apr-05 2:06 
QuestionAny chance of translating the code? Pin
mppeters13-Apr-05 10:01
membermppeters13-Apr-05 10:01 
AnswerRe: Any chance of translating the code? Pin
scubaduba13-Apr-05 11:35
memberscubaduba13-Apr-05 11:35 
GeneralMil gracias Pin
sirenita12-Apr-05 7:17
membersirenita12-Apr-05 7:17 
GeneralRe: Mil gracias Pin
scubaduba12-Apr-05 9:55
memberscubaduba12-Apr-05 9:55 
GeneralRe: Mil gracias Pin
AliWC28-Feb-06 1:58
memberAliWC28-Feb-06 1:58 
GeneralGreat Menu! Pin
shelmark31-Mar-05 10:33
membershelmark31-Mar-05 10:33 
GeneralRe: Great Menu! Pin
scubaduba8-Apr-05 12:03
memberscubaduba8-Apr-05 12:03 
QuestionStatically placing the menu? Pin
Jonathan Merriweather31-Mar-05 3:49
memberJonathan Merriweather31-Mar-05 3:49 
AnswerRe: Statically placing the menu? Pin
Hlynur þór5-Apr-05 6:31
memberHlynur þór5-Apr-05 6:31 
GeneralSQL Implementation Pin
AlexSz29-Mar-05 21:35
memberAlexSz29-Mar-05 21:35 
GeneralRe: SQL Implementation Pin
scubaduba29-Mar-05 23:18
memberscubaduba29-Mar-05 23:18 
GeneralOutstanding Menu Pin
eyespike125-Mar-05 5:42
membereyespike125-Mar-05 5:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 11 Apr 2005
Article Copyright 2005 by scubaduba
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid