Click here to Skip to main content
Click here to Skip to main content

Database Driven HTML popup menu

By , 27 Oct 2005
Rate this:
Please Sign up or sign in to vote.

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:

//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:

<!--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

About the Author

Mugombi
Web Developer
South Africa South Africa
No Biography provided

Comments and Discussions

 
GeneralConnecting the menu to a different database Pinmemberb0rnslipy7-Jul-08 21:42 
GeneralDiplaying database (in C#) in website (in html) Pinmember22teddy2226-Mar-07 7:37 
Generaldrop down lists Pinmembereli_b22-May-06 6:48 
GeneralRe: drop down lists PinmemberMugombi25-May-06 2:49 
GeneralQuestion about positioning Pinmemberjacobstrix4-May-06 19:44 
GeneralRe: Question about positioning PinmemberMugombi8-May-06 6:15 
Generaltop.ascx does not work PinmemberSergeiT6-Dec-05 10:08 
GeneralRe: top.ascx does not work PinmemberSergeiT6-Dec-05 11:32 
GeneralExcellent Menu Pinmembermetweek4-Nov-05 9:48 
GeneralThere is already a good component for this :-) Pinmemberpnr1-Nov-05 19:55 
GeneralRe: There is already a good component for this :-) PinmemberMugombi2-Nov-05 21:41 
GeneralRe: There is already a good component for this :-) Pinmemberpnr2-Nov-05 22:10 
GeneralRe: There is already a good component for this :-) PinmemberMugombi2-Nov-05 23:31 
GeneralRe: There is already a good component for this :-) PinmemberNazf9-Nov-05 10:54 
GeneralRe: There is already a good component for this :-) PinmemberMugombi10-Nov-05 1:06 

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 | Mobile
Web04 | 2.8.140415.2 | Last Updated 27 Oct 2005
Article Copyright 2005 by Mugombi
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid