Click here to Skip to main content
15,885,869 members
Articles / Web Development / HTML
Article

Database Driven HTML popup menu

Rate me:
Please Sign up or sign in to vote.
3.35/5 (8 votes)
27 Oct 20051 min read 98.2K   3.2K   54   15
An article on how to generate an HTML popup menu from a database.

Sample Image

Introduction

A while ago my boss approached me to do a content management system for a company that wanted to have drop down menus for sub-links on their websites. I went on the net to try and find a control that could make me easily achieve that but to my surprise I found nothing! This inspired me to write a Dreamweaver like popup menu control that gets parent and child nodes from the database and creates a JavaScript drop down menu just like the one you get in the Macromedia Dreamweaver.

Using the code

I created a class library (attached) that has two classes. The class dvstories returns a DataView of the main/sub pages listing. The second, buildmenu - returns a string with JavaScript for building the parent and child nodes of the menu.

In the header file for all my .aspx pages - top.ascx - I construct a string to raise the MouseOver event for the menus to popup. I also hard-coded (for this version) the menu dimensions which determine where the menu is displayed:

C#
//mouseover event
//refer to mm_menu.js to understand the variables
string mnustr = "onMouseOver=\"MM_showMenu(window.mnmenu," + 
       "x,138,null,'image2')\" onMouseOut=\"MM_startTimeout();\"";
sect2 = "";
sect3 = "";
sect4 = "";
sect6 = "";
//location of menu - y axis
int x = 160;

try 
{
    DataView dvmainlist = new DataView();
    DataView subs = new DataView();

    dvmainlist = dp.dvstories(1);

    //start building menu children from parent nodes
    //also puts in dimensions of the menu dropdown....

    foreach (DataRow r in dvmainlist.Table.Rows)
    {
        string mainuid = r["main_pgID"].ToString();
        if (Convert.ToInt32(mainuid) != dp.sub_id)
        {
            subs = fillchild("2");

            if (subs.Table.Rows.Count >= 1 )
            {
                sect2 = mnustr.Replace("mnmenu", "mm_menu_2_0");
                sect2 = sect2.Replace("x", x.ToString());
            }

            subs = fillchild("3");

            if (subs.Table.Rows.Count >= 1 )
            {
                sect3 = mnustr.Replace("mnmenu", "mm_menu_3_0");
                //location of menu - y-axis
                sect3 = sect3.Replace("x", (x + 185).ToString());
            }

            subs = fillchild("4");

            if (subs.Table.Rows.Count >= 1 )
            {
                sect4 = mnustr.Replace("mnmenu", "mm_menu_4_0");
                sect4 = sect4.Replace("x", (x + 290).ToString());
            }

            subs = fillchild("6");

            if (subs.Table.Rows.Count >= 1 )
            {
                sect6 = mnustr.Replace("mnmenu", "mm_menu_6_0");
                sect6 = sect6.Replace("x",(x + 300).ToString());
            }
    
        }

    }
}
catch (Exception ex)
{
    Response.Write(ex.ToString());
}

Within the HTML section of the actual .aspx (default.aspx), for the example page, I had to put the following code:

JavaScript
<!--put this within the head tag -->
<script language="JavaScript">
<!--
function mmLoadMenus() {
    <%=menucontents%>
} // mmLoadMenus()
//-->
</script>
<script language="JavaScript" src="mm_menu.js"></script>
<!--put this after the <head> tag -->
<script language="JavaScript">mmLoadMenus();</script>

In the code-behind file, you have to call the buildmenu class from the component in order to retain the menu content string.

History

I am currently working on making this a control fully customizable - attributes like back ground color, x,y position etc. Any help is very much appreciated.

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


Written By
Web Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralConnecting the menu to a different database Pin
b0rnslipy7-Jul-08 21:42
b0rnslipy7-Jul-08 21:42 
GeneralDiplaying database (in C#) in website (in html) Pin
22teddy2226-Mar-07 7:37
22teddy2226-Mar-07 7:37 
Generaldrop down lists Pin
eli_b22-May-06 6:48
eli_b22-May-06 6:48 
GeneralRe: drop down lists Pin
Mugombi25-May-06 2:49
Mugombi25-May-06 2:49 
GeneralQuestion about positioning Pin
jacobstrix4-May-06 19:44
jacobstrix4-May-06 19:44 
GeneralRe: Question about positioning Pin
Mugombi8-May-06 6:15
Mugombi8-May-06 6:15 
Generaltop.ascx does not work Pin
SergeiT6-Dec-05 10:08
SergeiT6-Dec-05 10:08 
GeneralRe: top.ascx does not work Pin
SergeiT6-Dec-05 11:32
SergeiT6-Dec-05 11:32 
GeneralExcellent Menu Pin
metweek4-Nov-05 9:48
metweek4-Nov-05 9:48 
GeneralThere is already a good component for this :-) Pin
pnr1-Nov-05 19:55
pnr1-Nov-05 19:55 
GeneralRe: There is already a good component for this :-) Pin
Mugombi2-Nov-05 21:41
Mugombi2-Nov-05 21:41 
GeneralRe: There is already a good component for this :-) Pin
pnr2-Nov-05 22:10
pnr2-Nov-05 22:10 
GeneralRe: There is already a good component for this :-) Pin
Mugombi2-Nov-05 23:31
Mugombi2-Nov-05 23:31 
GeneralRe: There is already a good component for this :-) Pin
Nazf9-Nov-05 10:54
Nazf9-Nov-05 10:54 
GeneralRe: There is already a good component for this :-) Pin
Mugombi10-Nov-05 1:06
Mugombi10-Nov-05 1:06 

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

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