如何实现在Asp.net下XP风格的下拉菜单

结合Jst和Css实现了在Asp.net下XP风格的下拉菜单,效果图如下:

aaa.gif

bbb.gif

实现的步骤及源码如下:
Default.aspx代码:

 1 ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="PopupMenuControlSample._default"  %>
 2 None.gif <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
 3 None.gif < HTML >
 4 None.gif     < HEAD >
 5 None.gif         < title > PopupMenu control sample </ title >
 6 None.gif         < meta  content ="Microsoft Visual Studio .NET 7.1"  name ="GENERATOR" >
 7 None.gif         < meta  content ="C#"  name ="CODE_LANGUAGE" >
 8 None.gif         < meta  content ="JavaScript"  name ="vs_defaultClientScript" >
 9 None.gif         < meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
10 None.gif         < LINK  href ="PopupMenuSample.css"  type ="text/css"  rel ="Stylesheet" >
11 None.gif     </ HEAD >
12 None.gif     < body  bgColor ="#ffffcc"  MS_POSITIONING ="GridLayout" >
13 None.gif         < form  id ="MainForm"  method ="post"  runat ="server" >
14 None.gif             < asp:label  id ="PopupMenuOwner"  Runat ="server"  CssClass ="MainText" > Click on me to show the popup menu </ asp:label >
15 None.gif         </ form >
16 None.gif     </ body >
17 None.gif </ HTML >
18 None.gif



Default.aspx.cs代码:

  1 None.gif using  System;
  2 None.gif using  System.Collections;
  3 None.gif using  System.ComponentModel;
  4 None.gif using  System.Data;
  5 None.gif using  System.Drawing;
  6 None.gif using  System.Web;
  7 None.gif using  System.Web.SessionState;
  8 None.gif using  System.Web.UI;
  9 None.gif using  System.Web.UI.WebControls;
 10 None.gif using  System.Web.UI.HtmlControls;
 11 None.gif using  UIControl;
 12 None.gif
 13 None.gif namespace  PopupMenuControlSample
 14 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 16InBlock.gif    /// Summary description for _default.
 17ExpandedSubBlockEnd.gif    /// </summary>

 18InBlock.gif    public class _default : System.Web.UI.Page
 19ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 20InBlock.gif        const string ControlContainer = "MainForm";
 21InBlock.gif        const string ClickedMessage = "<BR/><BR/>The menu item '{0}' has been selected by the user.";
 22InBlock.gif        const string NoMenuItemClicked = "<BR/><BR/>No menu item has been clicked yet.";
 23InBlock.gif        const string MessageStyle = "Message";
 24InBlock.gif        const string MenuControlID = "PopupMenu";
 25InBlock.gif        const MenuItemDirection MenuDirection = MenuItemDirection.Vertical;
 26InBlock.gif        const string PopupMenuParentID = "PopupMenuOwner";
 27InBlock.gif        const string Services = "Services";
 28InBlock.gif        const string ServicesAltText = "available services";
 29InBlock.gif        const string ServicesName = "Services";
 30InBlock.gif        const string ServicesJavaScript = null;
 31InBlock.gif        const string ServicesImageUrl = null;
 32InBlock.gif        const MenuItemDirection ServicesDirection = MenuItemDirection.Vertical;
 33InBlock.gif        const string ServicesTableID = "Services";
 34InBlock.gif        const string AboutMe = "About me";
 35InBlock.gif        const string AboutMeAltText = "about me";
 36InBlock.gif        const string AboutMeName = "AboutMe";
 37InBlock.gif        const string AboutMeJavaScript = null;
 38InBlock.gif        const string AboutMeImageUrl = null;
 39InBlock.gif        const MenuItemDirection AboutMeDirection = MenuItemDirection.Vertical;
 40InBlock.gif        const string AboutMeTableID = "AboutMe";
 41InBlock.gif        const string UsefulLinks = "Useful links";
 42InBlock.gif        const string UsefulLinksAltText = "useful links";
 43InBlock.gif        const string UsefulLinksName = "UsefulLinks";
 44InBlock.gif        const string UsefulLinksJavaScript = null;
 45InBlock.gif        const string UsefulLinksImageUrl = null;
 46InBlock.gif        const MenuItemDirection UsefulLinksDirection = MenuItemDirection.Vertical;
 47InBlock.gif        const string UsefulLinksTableID = "UsefulLinks";
 48InBlock.gif        const string Presenter = ".NET Presenter";
 49InBlock.gif        const string PresenterAltText = "presenter for your .NET usergroup or show";
 50InBlock.gif        const string PresenterName = "Presenter";
 51InBlock.gif        const string PresenterJavaScript = null;
 52InBlock.gif        const string PresenterImageUrl = "images/presenter.gif";
 53InBlock.gif        const string PresenterTableID = "Presenter";
 54InBlock.gif        const string Author = ".NET author";
 55InBlock.gif        const string AuthorAltText = "author for your .NET magazine";
 56InBlock.gif        const string AuthorName = "Author";
 57InBlock.gif        const string AuthorJavaScript = "window.alert('Klaus Salchner, Chief Enterprise Architect');";
 58InBlock.gif        const string AuthorImageUrl = null;
 59InBlock.gif        const string AuthorTableID = "Author";
 60InBlock.gif        const string Architect = ".NET architecture review";
 61InBlock.gif        const string ArchitectAltText = "architecture review for your .NET project";
 62InBlock.gif        const string ArchitectName = "Architect";
 63InBlock.gif        const string ArchitectJavaScript = null;
 64InBlock.gif        const string ArchitectImageUrl = "images/review.gif";
 65InBlock.gif        const string ArchitectTableID = "Architect";
 66InBlock.gif        const string Resume = "Resume";
 67InBlock.gif        const string ResumeAltText = "my resume";
 68InBlock.gif        const string ResumeName = "Resume";
 69InBlock.gif        const string ResumeJavaScript = null;
 70InBlock.gif        const string ResumeImageUrl = null;
 71InBlock.gif        const string ResumeTableID = "Resume";
 72InBlock.gif        const string ContactMe = "Contact me";
 73InBlock.gif        const string ContactMeAltText = "contact me";
 74InBlock.gif        const string ContactMeName = "ContactMe";
 75InBlock.gif        const string ContactMeJavaScript = null;
 76InBlock.gif        const string ContactMeImageUrl = "images/contactme.gif";
 77InBlock.gif        const string ContactMeTableID = "ContactMe";
 78InBlock.gif        const string CodeGuru = "CodeGuru";
 79InBlock.gif        const string CodeGuruAltText = "CodeGuru";
 80InBlock.gif        const string CodeGuruName = "CodeGuru";
 81InBlock.gif        const string CodeGuruJavaScript = null;
 82InBlock.gif        const string CodeGuruImageUrl = "images/codeguru.gif";
 83InBlock.gif        const string CodeGuruTableID = "CodeGuru";
 84InBlock.gif        const string DeveloperLand = "DeveloperLand";
 85InBlock.gif        const string DeveloperLandAltText = "DeveloperLand";
 86InBlock.gif        const string DeveloperLandName = "DeveloperLand";
 87InBlock.gif        const string DeveloperLandJavaScript = null;
 88InBlock.gif        const string DeveloperLandImageUrl = "images/developerland.gif";
 89InBlock.gif        const string DeveloperLandTableID = "DeveloperLand";
 90InBlock.gif        const string Msdn = "MSDN";
 91InBlock.gif        const string MsdnAltText = "MSDN";
 92InBlock.gif        const string MsdnName = "Msdn";
 93InBlock.gif        const string MsdnJavaScript = null;
 94InBlock.gif        const string MsdnImageUrl = "images/msdn.gif";
 95InBlock.gif        const string MsdnTableID = "Msdn";
 96InBlock.gif        const string GotDotNet = "Got-Dot-Net";
 97InBlock.gif        const string GotDotNetAltText = "Got-Dot-Net";
 98InBlock.gif        const string GotDotNetName = "GotDotNet";
 99InBlock.gif        const string GotDotNetJavaScript = null;
100InBlock.gif        const string GotDotNetImageUrl = null;
101InBlock.gif        const string GotDotNetTableID = "GotDotNet";
102InBlock.gif        private Label Message;
103InBlock.gif        protected System.Web.UI.WebControls.Label PopupMenuOwner;
104InBlock.gif
105ContractedSubBlock.gifExpandedSubBlockStart.gif        Web Form Designer generated code#region Web Form Designer generated code
106InBlock.gif        override protected void OnInit(EventArgs e)
107ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
108InBlock.gif            //
109InBlock.gif            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
110InBlock.gif            //
111InBlock.gif            InitializeComponent();
112InBlock.gif            base.OnInit(e);
113ExpandedSubBlockEnd.gif        }

114InBlock.gif        
115ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
116InBlock.gif        /// Required method for Designer support - do not modify
117InBlock.gif        /// the contents of this method with the code editor.
118ExpandedSubBlockEnd.gif        /// </summary>

119InBlock.gif        private void InitializeComponent()
120ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
121InBlock.gif            this.Init += new System.EventHandler(this.Page_Init);
122InBlock.gif
123ExpandedSubBlockEnd.gif        }

124ExpandedSubBlockEnd.gif        #endregion

125InBlock.gif
126ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
127InBlock.gif        /// initialize the form; allows us to add new controls to it
128InBlock.gif        /// </summary>
129InBlock.gif        /// <param name="sender">event sender</param>
130ExpandedSubBlockEnd.gif        /// <param name="e">event argument</param>

131InBlock.gif        private void Page_Init(object sender, System.EventArgs e)
132ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
133InBlock.gif            // create a new popup menu control to display
134InBlock.gif            PopupMenuControl MyPopupMenu = new PopupMenuControl();
135InBlock.gif            MyPopupMenu.MenuDirection = MenuDirection;
136InBlock.gif            MyPopupMenu.MenuShowSubMenuIndicator = true;
137InBlock.gif            MyPopupMenu.PopupMenuParentID = PopupMenuParentID;
138InBlock.gif            MyPopupMenu.ShowVerticalMenuImageBar = true;
139InBlock.gif            MyPopupMenu.ID = MenuControlID;
140InBlock.gif
141InBlock.gif            // create the popup-menu items
142InBlock.gif            MyPopupMenu.PopupMenu.Add(new PopupMenuItemDetails(Services, ServicesAltText, ServicesName, ServicesJavaScript, ServicesImageUrl, null, ServicesDirection, ServicesTableID, true));
143InBlock.gif            MyPopupMenu.PopupMenu.Add(new PopupMenuItemDetails(AboutMe, AboutMeAltText, AboutMeName, AboutMeJavaScript, AboutMeImageUrl, null, AboutMeDirection, AboutMeTableID, true));
144InBlock.gif            MyPopupMenu.PopupMenu.Add(new PopupMenuItemDetails(UsefulLinks, UsefulLinksAltText, UsefulLinksName, UsefulLinksJavaScript, UsefulLinksImageUrl, null, UsefulLinksDirection, UsefulLinksTableID, true));
145InBlock.gif
146InBlock.gif            // create the services sub-menu
147InBlock.gif            MyPopupMenu.PopupMenu[0].PopupSubMenus = new PopupMenuItemDetailsCollection();
148InBlock.gif            MyPopupMenu.PopupMenu[0].PopupSubMenus.Add(new PopupMenuItemDetails(Presenter, PresenterAltText, PresenterName, PresenterJavaScript, PresenterImageUrl, null, MenuItemDirection.Vertical, PresenterTableID, false));
149InBlock.gif            MyPopupMenu.PopupMenu[0].PopupSubMenus.Add(new PopupMenuItemDetails(Author, AuthorAltText, AuthorName, AuthorJavaScript, AuthorImageUrl, null, MenuItemDirection.Vertical, AuthorTableID, false));
150InBlock.gif            MyPopupMenu.PopupMenu[0].PopupSubMenus.Add(new PopupMenuItemDetails(Architect, ArchitectAltText, ArchitectName, ArchitectJavaScript, ArchitectImageUrl, null, MenuItemDirection.Vertical, ArchitectTableID, false));
151InBlock.gif
152InBlock.gif            // create the about-me sub-menu
153InBlock.gif            MyPopupMenu.PopupMenu[1].PopupSubMenus = new PopupMenuItemDetailsCollection();
154InBlock.gif            MyPopupMenu.PopupMenu[1].PopupSubMenus.Add(new PopupMenuItemDetails(Resume, ResumeAltText, ResumeName, ResumeJavaScript, ResumeImageUrl, null, MenuItemDirection.Vertical, ResumeTableID, false));
155InBlock.gif            MyPopupMenu.PopupMenu[1].PopupSubMenus.Add(new PopupMenuItemDetails(ContactMe, ContactMeAltText, ContactMeName, ContactMeJavaScript, ContactMeImageUrl, null, MenuItemDirection.Vertical, ContactMeTableID, false));
156InBlock.gif
157InBlock.gif            // create the useful links sub-menu
158InBlock.gif            MyPopupMenu.PopupMenu[2].PopupSubMenus = new PopupMenuItemDetailsCollection();
159InBlock.gif            MyPopupMenu.PopupMenu[2].PopupSubMenus.Add(new PopupMenuItemDetails(DeveloperLand, DeveloperLandAltText, DeveloperLandName, DeveloperLandJavaScript, DeveloperLandImageUrl, null, MenuItemDirection.Vertical, DeveloperLandTableID, false));
160InBlock.gif            MyPopupMenu.PopupMenu[2].PopupSubMenus.Add(new PopupMenuItemDetails(CodeGuru, CodeGuruAltText, CodeGuruName, CodeGuruJavaScript, CodeGuruImageUrl, null, MenuItemDirection.Vertical, CodeGuruTableID, false));
161InBlock.gif            MyPopupMenu.PopupMenu[2].PopupSubMenus.Add(new PopupMenuItemDetails(GotDotNet, GotDotNetAltText, GotDotNetName, GotDotNetJavaScript, GotDotNetImageUrl, null, MenuItemDirection.Vertical, GotDotNetTableID, false));
162InBlock.gif            MyPopupMenu.PopupMenu[2].PopupSubMenus.Add(PopupMenuItemDetails.PopupMenuItemSeparator());
163InBlock.gif            MyPopupMenu.PopupMenu[2].PopupSubMenus.Add(new PopupMenuItemDetails(Msdn, MsdnAltText, MsdnName, MsdnJavaScript, MsdnImageUrl, null, MenuItemDirection.Vertical, MsdnTableID, false));
164InBlock.gif
165InBlock.gif            // find the cell we use to add our content
166InBlock.gif            Control ContentCell = BaseControl.FindControl(Controls, ControlContainer);
167InBlock.gif
168InBlock.gif            // event handler called when a menu item has been clicked
169InBlock.gif            MyPopupMenu.MenuClicked += new MenuClicked(MyPopupMenu_MenuClicked);
170InBlock.gif
171InBlock.gif            // if we found the content cell then let's add the popup menu control to it
172InBlock.gif            if (ContentCell != null)
173InBlock.gif                ContentCell.Controls.Add(MyPopupMenu);
174InBlock.gif
175InBlock.gif            // the label which shows which menu item has been clicked
176InBlock.gif            Message = BaseControl.CreateLabel(Controls, NoMenuItemClicked);
177InBlock.gif            Message.CssClass = MessageStyle;
178ExpandedSubBlockEnd.gif        }

179InBlock.gif
180ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
181InBlock.gif        /// event handler called when a menu item has been clicked
182InBlock.gif        /// </summary>
183InBlock.gif        /// <param name="Controls">the menu controls collection</param>
184ExpandedSubBlockEnd.gif        /// <param name="ClickedMenuItem">the selected menu item</param>

185InBlock.gif        void MyPopupMenu_MenuClicked(ControlCollection Controls, MenuItemDetails ClickedMenuItem)
186ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
187InBlock.gif            Message.Text = String.Format(ClickedMessage, ClickedMenuItem.DisplayText);
188ExpandedSubBlockEnd.gif        }
    
189ExpandedSubBlockEnd.gif    }

190ExpandedBlockEnd.gif}

191 None.gif

PopupMenuControl.js代码:

  1 None.gif
  2 None.gif var  VisibleSubMenus  =  '';
  3 None.gif var  MenuTimer  =   0 ;
  4 None.gif var  CloseTimer  =   0 ;
  5 None.gif
  6 None.gif
  7 None.gif function  EnterMenuCell(MenuCell,SubMenuTable)
  8 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  9InBlock.gif    // if the sub-menu-table is not displayed at the moment
 10InBlock.gif    if (SubMenuTable.style.display == 'none')
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12InBlock.gif        // then display it now
 13InBlock.gif        SubMenuTable.style.display = '';
 14InBlock.gif
 15InBlock.gif        // add this to the list of visible sub-menus; this is important if the user browses
 16InBlock.gif        // multiple cascaded sub-menus
 17InBlock.gif        if (VisibleSubMenus.length == 0)
 18InBlock.gif            VisibleSubMenus += SubMenuTable.id;
 19InBlock.gif        else
 20InBlock.gif            VisibleSubMenus += ',' + SubMenuTable.id;
 21ExpandedSubBlockEnd.gif    }

 22ExpandedBlockEnd.gif}

 23 None.gif
 24 None.gif //
 25 None.gif//  the user moves the mouse outside the menu cell
 26 None.gif//
 27 None.gif function  LeaveMenuCell(MenuCell,SubMenuTable)
 28 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 29InBlock.gif   
 30InBlock.gif    if (MenuTimer == 0)
 31InBlock.gif        MenuTimer = window.setInterval("CheckIfSubMenuToClose(" + SubMenuTable.id + ")"20);
 32ExpandedBlockEnd.gif}

 33 None.gif
 34 None.gif
 35 None.gif function  LeaveSubMenuTable(SubMenuTable)
 36 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 37InBlock.gif    // get the list of visible sub-menus
 38InBlock.gif    var VisibleSubMenusArray = VisibleSubMenus.split(",");
 39InBlock.gif
 40InBlock.gif    // get the last visible sub-menu
 41InBlock.gif    var LastVisibleSubMenu = VisibleSubMenusArray[VisibleSubMenusArray.length - 1];
 42InBlock.gif
 43InBlock.gif
 44InBlock.gif    if ((SubMenuTable.style.display != 'none') & (LastVisibleSubMenu == SubMenuTable.id))
 45ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 46InBlock.gif        // closes the sub-menu table
 47InBlock.gif        CloseSubMenu(SubMenuTable);
 48InBlock.gif
 49InBlock.gif        
 50InBlock.gif        if (MenuTimer == 0)
 51InBlock.gif            MenuTimer = window.setInterval("CloseAllSubMenus()"20);
 52ExpandedSubBlockEnd.gif    }

 53ExpandedBlockEnd.gif}

 54 None.gif
 55 None.gif
 56 None.gif function  CloseAllSubMenus()
 57 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 58InBlock.gif    // first clear the timer
 59InBlock.gif    if (MenuTimer != 0)
 60ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 61InBlock.gif       window.clearInterval(MenuTimer);
 62InBlock.gif       MenuTimer = 0;
 63ExpandedSubBlockEnd.gif    }

 64InBlock.gif    
 65InBlock.gif    // check if we have any sub-menus open
 66InBlock.gif    if (VisibleSubMenus.length > 0)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 68InBlock.gif        // get the list of visible sub-menus
 69InBlock.gif        var VisibleSubMenusArray = VisibleSubMenus.split(",");
 70InBlock.gif
 71InBlock.gif        // now loop through all visible sub-menus and close them too
 72InBlock.gif        for (Count=0; Count < VisibleSubMenusArray.length; Count++)
 73ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 74InBlock.gif            // get a reference to the visible sub-menu
 75InBlock.gif            var SubMenu = document.getElementById(VisibleSubMenusArray[Count]);
 76InBlock.gif
 77InBlock.gif            // and now hide it
 78InBlock.gif            SubMenu.style.display = 'none';
 79ExpandedSubBlockEnd.gif        }

 80InBlock.gif
 81InBlock.gif        // reset the list of visible sub-menus
 82InBlock.gif        VisibleSubMenus = '';
 83ExpandedSubBlockEnd.gif    }

 84ExpandedBlockEnd.gif}

 85 None.gif
 86 None.gif //
 87 None.gif//  the user moves the mosue cursor over the sub-menu table
 88 None.gif//
 89 None.gif function  EnterSubMenuTable(SubMenuTable)
 90 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 91InBlock.gif   
 92InBlock.gif    if (MenuTimer != 0)
 93ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 94InBlock.gif        window.clearInterval(MenuTimer);
 95InBlock.gif        MenuTimer = 0;
 96ExpandedSubBlockEnd.gif    }

 97ExpandedBlockEnd.gif}

 98 None.gif
 99 None.gif
100 None.gif function  CheckIfSubMenuToClose(SubMenuTable)
101 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
102InBlock.gif    // first clear the timer itself
103InBlock.gif    if (MenuTimer != 0)
104ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
105InBlock.gif       window.clearInterval(MenuTimer);
106InBlock.gif       MenuTimer = 0;
107ExpandedSubBlockEnd.gif    }

108InBlock.gif
109InBlock.gif    // now close the sub-menu table
110InBlock.gif    CloseSubMenu(SubMenuTable);
111ExpandedBlockEnd.gif}

112 None.gif
113 None.gif //
114 None.gif//  closes the sub-menu table and removes it from the list of visible sub-menus
115 None.gif//
116 None.gif function  CloseSubMenu(SubMenuTable)
117 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
118InBlock.gif    // now close the sub-menu
119InBlock.gif    SubMenuTable.style.display = 'none';
120InBlock.gif
121InBlock.gif    // get the list of visible sub-menus
122InBlock.gif    var VisibleSubMenusArray = VisibleSubMenus.split(",");
123InBlock.gif
124InBlock.gif    // now we rebuild the list of visible sub-menus by excluding the sub-menu we 
125InBlock.gif    // just closed
126InBlock.gif    VisibleSubMenus = '';
127InBlock.gif
128InBlock.gif    // loop through all existing sub-menus
129InBlock.gif    for (Count=0; Count < VisibleSubMenusArray.length; Count++)
130InBlock.gif
131InBlock.gif        // if the sub-menu is the same as the one we just closed then ignore it;
132InBlock.gif        // otherwise add it again
133InBlock.gif        if (VisibleSubMenusArray[Count] != SubMenuTable.id)
134InBlock.gif            if (VisibleSubMenus.length > 0)
135InBlock.gif                VisibleSubMenus += "," + VisibleSubMenusArray[Count];
136InBlock.gif            else
137InBlock.gif                VisibleSubMenus = VisibleSubMenusArray[Count];
138ExpandedBlockEnd.gif}

139 None.gif
140 None.gif
141 None.gif function  SetControlPosition(SubMenuTable,ParentTable,ParentCell,ParentMenuDirection,OffsetX,OffsetY)
142 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
143InBlock.gif    // we position the control absolute
144InBlock.gif    SubMenuTable.style.position = 'absolute';
145InBlock.gif
146InBlock.gif    // parent menu is horizontal, so we position the sub-menu at left/bottom
147InBlock.gif    if (ParentMenuDirection == 'Horizontal')
148ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
149InBlock.gif        SubMenuTable.style.left = ParentTable.offsetLeft + ParentCell.offsetLeft - 1;
150InBlock.gif        SubMenuTable.style.top = ParentTable.offsetTop + ParentTable.offsetHeight - 1;
151ExpandedSubBlockEnd.gif    }

152InBlock.gif    else
153InBlock.gif
154InBlock.gif        // parent menu is vertical, so we position the sub-menu at right/top
155InBlock.gif        if (ParentMenuDirection == 'Vertical')
156ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
157InBlock.gif            SubMenuTable.style.left = ParentTable.offsetLeft + ParentTable.offsetWidth - 1;
158InBlock.gif            SubMenuTable.style.top = ParentTable.offsetTop + ParentCell.offsetTop - 1;
159ExpandedSubBlockEnd.gif        }

160InBlock.gif
161InBlock.gif        // there is no parent-menu; this is a popup menu which will be positioned in the
162InBlock.gif        // middle of the owning control
163InBlock.gif        else
164ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
165InBlock.gif            SubMenuTable.style.left = ParentTable.offsetLeft + (ParentTable.offsetWidth / 2+ 1 + Number(OffsetX);
166InBlock.gif            SubMenuTable.style.top = ParentTable.offsetTop + (ParentTable.offsetHeight / 2+ 1 + Number(OffsetY);
167InBlock.gif
168InBlock.gif            // add any body margin we have
169InBlock.gif            SubMenuTable.style.left = Number(SubMenuTable.style.left.replace('px','')) + Number(document.body.leftMargin);
170InBlock.gif            SubMenuTable.style.top = Number(SubMenuTable.style.top.replace('px','')) + Number(document.body.topMargin);
171ExpandedSubBlockEnd.gif        }

172ExpandedBlockEnd.gif}

173 None.gif
174 None.gif
175 None.gif function  ShowPopupMenu(PopupOwnerControl,PopupTableControl,OffsetX,OffsetY)
176 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
177InBlock.gif    // position the popup menu in the middle of the owner control
178InBlock.gif    SetControlPosition(PopupTableControl, PopupOwnerControl, null, 'popup', OffsetX, OffsetY);
179InBlock.gif
180InBlock.gif    // show the popup-menu
181InBlock.gif    PopupTableControl.style.display = '';
182InBlock.gif
183InBlock.gif    // give the popup menu table the focus so we know when the user clicks anywhere
184InBlock.gif    // else (the control looses then the focus
185InBlock.gif    PopupTableControl.focus();
186ExpandedBlockEnd.gif}

187 None.gif
188 None.gif
189 None.gif function  ClosePopupMenu(PopupOwnerControl,PopupTableControl)
190 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
191InBlock.gif    if (CloseTimer == 0)
192InBlock.gif        CloseTimer = window.setInterval("ClosePopupMenuDelayed(" + PopupOwnerControl.id + "," + PopupTableControl.id + ")"190);
193ExpandedBlockEnd.gif}

194 None.gif
195 None.gif
196 None.gif function  ClosePopupMenuDelayed(PopupOwnerControl,PopupTableControl)
197 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
198InBlock.gif    // clears first the timer
199InBlock.gif    if (CloseTimer != 0)
200ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
201InBlock.gif        window.clearInterval(CloseTimer);
202InBlock.gif        CloseTimer = 0;
203ExpandedSubBlockEnd.gif    }

204InBlock.gif
205InBlock.gif    // we hide the popup menu
206InBlock.gif    PopupTableControl.style.display = 'none';
207InBlock.gif
208InBlock.gif    // close any open sub-menu
209InBlock.gif    CloseAllSubMenus();
210ExpandedBlockEnd.gif}

211 None.gif

PopupMenuControl.css代码:

 1 None.gif .PopupMenuTableStyle
 2 ExpandedBlockStart.gifContractedBlock.gif {dot.gif} {
 3InBlock.gif    border: groove 2px white;
 4InBlock.gif    background-color: black;
 5InBlock.gif    position: relative;
 6InBlock.gif    left: 0px;
 7InBlock.gif    top: 0px;
 8ExpandedBlockEnd.gif}

 9 None.gif.PopupMenuRowStyle
10 ExpandedBlockStart.gifContractedBlock.gif {dot.gif} {
11InBlock.gif    vertical-align: middle;
12ExpandedBlockEnd.gif}

13 None.gif.PopupMenuCellStyle
14 ExpandedBlockStart.gifContractedBlock.gif {dot.gif} {
15InBlock.gif    background-color: #ececec;
16InBlock.gif    text-align: left;
17InBlock.gif    width: 180px;
18InBlock.gif    height: 20px;
19ExpandedBlockEnd.gif}

20 None.gif.PopupMenuCellHighlightedStyle
21 ExpandedBlockStart.gifContractedBlock.gif {dot.gif} {
22InBlock.gif    background-color: #ccccff;
23InBlock.gif    text-align: left;
24InBlock.gif    width: 180px;
25InBlock.gif    height: 20px;
26ExpandedBlockEnd.gif}

27 None.gif.PopupMenuLinkStyle
28 ExpandedBlockStart.gifContractedBlock.gif {dot.gif} {
29InBlock.gif    font: normal normal bold small/normal Arial;
30InBlock.gif    text-decoration: none;
31InBlock.gif    white-space: nowrap;
32InBlock.gif    color: blue;
33InBlock.gif    cursor: hand;
34InBlock.gif    line-height: 18px;
35ExpandedBlockEnd.gif}

36 None.gif.PopupMenuLinkHighlightedStyle
37 ExpandedBlockStart.gifContractedBlock.gif {dot.gif} {
38InBlock.gif    font: normal normal bolder small/normal Arial;
39InBlock.gif    text-decoration: none;
40InBlock.gif    white-space: nowrap;
41InBlock.gif    color: #ffffcc;
42InBlock.gif    color: blue;
43InBlock.gif    cursor: hand;
44InBlock.gif    line-height: 18px;
45ExpandedBlockEnd.gif}

46 None.gif.MenuSeparatorCellStyle
47 ExpandedBlockStart.gifContractedBlock.gif {dot.gif} {
48InBlock.gif    background-color: #ececec;
49InBlock.gif    height: 1px;
50InBlock.gif    padding: 0px 0px 0px 0px;
51InBlock.gif    margin: 0px 0px 0px 0px;
52ExpandedBlockEnd.gif}

53 None.gif.MenuSeparatorStyle
54 ExpandedBlockStart.gifContractedBlock.gif {dot.gif} {
55InBlock.gif    border-top: ridge 1px;
56InBlock.gif    width: 100%;
57InBlock.gif    color: white;
58InBlock.gif    height: 1px;
59ExpandedBlockEnd.gif}

60 None.gif.PopupMenuImageBarStyle
61 ExpandedBlockStart.gifContractedBlock.gif {dot.gif} {
62InBlock.gif    background-color: #cccccc;
63InBlock.gif    width: 24px;
64ExpandedBlockEnd.gif}

65 None.gif

源码下载地址:
http://files.cnblogs.com/Terrylee/PopupMenuControlSample.rar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值