手把手教你做下拉菜单[龙族首发][本人独创ajax下拉菜单][多个社区加为精华]
<P>手把手教你做下拉菜单[龙族首发][本人独创ajax下拉菜单]<BR>市面上讲述下拉菜单的文章多如牛毛,且使用下拉菜单的网站也非常之多,但在下看来,90%的网站和教程使用了如下代码:<BR></P><DIV class=htmlcode>
<P>function MM_reloadPage(init) { //reloads the window if Nav4 resized<BR> if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {<BR> document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}<BR> else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();<BR>}<BR>MM_reloadPage(true);<BR>// --></P>
<P>function MM_findObj(n, d) { //v4.0<BR> var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {<BR> d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}<BR> if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];<BR> for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);<BR> if(!x && document.getElementById) x=document.getElementById(n); return x;<BR>}</P>
<P>function MM_showHideLayers() { //v3.0<BR> var i,p,v,obj,args=MM_showHideLayers.arguments;<BR> for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];<BR> if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }<BR> obj.visibility=v; }<BR>}<BR></P></DIV>
<P>在下因公司的一个项目需要学习完ajax这个最流行的web技术之后,独辟蹊径想出一个集完全无刷新实时更新的下拉菜单,而且这个下拉菜单可以无限的可扩展性.下面请看源码:<BR>1.数据库,为了方便大家下载,在下所使用的是access,即想学习的朋友可以直接下载我包含数据库mdb文件的rar包,解压至IIS的发布目录即可直接查看效果.<BR>2.表结构如下图:</P>
<P>3.在下使用的是最传统的两层结构:<BR>数据库连接层DAL,Model数据模型层<BR>DAL db.cs源码:<BR></P>
<DIV class=htmlcode>
<P>using System;<BR>using System.Data;<BR>using System.Configuration;<BR>using System.Web;<BR>using System.Web.Security;<BR>using System.Web.UI;<BR>using System.Web.UI.WebControls;<BR>using System.Web.UI.WebControls.WebParts;<BR>using System.Web.UI.HtmlControls;<BR>using System.Data.OleDb;<BR>using System.IO;</P>
<P>/// <summary><BR>/// DB 的摘要说明<BR>/// </summary><BR>public class DB<BR>{<BR> private string strConn = System.Configuration.ConfigurationSettings.AppSettings["strConn"] + AppDomain.CurrentDomain.BaseDirectory + System.Configuration.ConfigurationSettings.AppSettings["strDBPath"];<BR> private OleDbConnection cn = null;<BR> public DB()<BR> {<BR> try<BR> {<BR> cn = new OleDbConnection(strConn);<BR> }<BR> catch (OleDbException oe)<BR> {<BR> Console.Write(oe.Message);<BR> }<BR> }</P>
<P> /// <summary><BR> /// 通过Sql脚本执行数据库增,删,改操作<BR> /// </summary><BR> /// <param name="strSql">Sql脚本</param><BR> /// <returns>true表示执行成功,false表示执行失败</returns><BR> public bool ExecCmdBySql(string strSql)<BR> {<BR> bool foo = true;</P>
<P> try<BR> {<BR> cn.Open();<BR> OleDbCommand cmd = new OleDbCommand();<BR> cmd.CommandText = strSql;<BR> cmd.Connection = cn;<BR> cmd.ExecuteNonQuery();<BR> }<BR> catch (OleDbException oe)<BR> {<BR> foo = false;<BR> Console.Write(oe.Message);<BR> }<BR> finally<BR> {<BR> cn.Close();//关闭接连<BR> }</P>
<P> return foo;<BR> <BR> }</P>
<P> <BR> /// <summary><BR> /// 通过查询得到数据集<BR> /// </summary><BR> /// <param name="strSql">查询Sql脚本</param><BR> /// <param name="ds">数据集引用</param><BR> /// <param name="tableName">表名</param><BR> /// <returns>true表示成功查询得到数据集,false表示查询失败</returns><BR> public bool GetDataSetByQuery(string strSql,ref DataSet ds,string tableName)<BR> {<BR> bool foo = true;<BR> try<BR> {<BR> cn.Open();<BR> OleDbCommand cmd = new OleDbCommand();<BR> cmd.CommandText = strSql;<BR> cmd.Connection = cn;<BR> OleDbDataReader reader = cmd.ExecuteReader();<BR> ds.Load(reader, LoadOption.OverwriteChanges, new string[] { tableName });<BR> }<BR> catch (OleDbException oe)<BR> {<BR> foo = false;<BR> Console.Write(oe.Message);<BR> }<BR> finally<BR> {<BR> cn.Close();<BR> }</P>
<P> return foo;<BR> }</P>
<P> /// <summary><BR> /// 通过查询得到首行数据编号<BR> /// </summary><BR> /// <param name="strSql">Sql脚本</param><BR> /// <param name="id">编号</param><BR> /// <returns></returns><BR> public bool GetScalarByQuery(string strSql, ref long id)<BR> {<BR> bool foo = true;<BR> try<BR> {<BR> cn.Open();<BR> OleDbCommand cmd = new OleDbCommand();<BR> cmd.CommandText = strSql;<BR> cmd.Connection = cn;<BR> Object obj = cmd.ExecuteScalar();<BR> if (!obj.ToString().Equals(""))<BR> {<BR> id = (long)obj;<BR> }<BR> else<BR> {<BR> id = 1;<BR> }<BR> }<BR> catch (OleDbException oe)<BR> {<BR> foo = false;<BR> Console.Write(oe.Message);<BR> }<BR> finally<BR> {<BR> cn.Close();<BR> }</P>
<P> return foo;<BR> }</P>
<P>}</P></DIV>
<P><BR>注:只考虑access数据库Sql连接的情况故有三个方法;</P>
<P>model层: CMenu类<BR><BR></P>
<DIV class=htmlcode>
<P>using System;<BR>using System.Data;<BR>using System.Configuration;<BR>using System.Web;<BR>using System.Web.Security;<BR>using System.Web.UI;<BR>using System.Web.UI.WebControls;<BR>using System.Web.UI.WebControls.WebParts;<BR>using System.Web.UI.HtmlControls;</P>
<P>/// <summary><BR>/// CMenu 的摘要说明<BR>/// </summary><BR>public class CMenu<BR>{<BR> private long menu_id; //菜单编号<BR> private string menu_name;//菜单名称<BR> private long menu_parentID;//父级菜单编号<BR> public long Menu_id<BR> {<BR> get { return menu_id; }<BR> set { menu_id = value; }<BR> }<BR> public string Menu_name<BR> {<BR> get { return menu_name; }<BR> set { menu_name = value; }<BR> }<BR> public long Menu_parentID<BR> {<BR> get { return menu_parentID; }<BR> set { menu_parentID = value; }<BR> }<BR> public CMenu()<BR> {<BR> menu_id = -1; //菜单编号<BR> menu_name = "";//菜单名称<BR> menu_parentID = -1;//父级菜单编号<BR> }</P>
<P> /// <summary><BR> /// 得到当前记录应该插入的编号<BR> /// </summary><BR> /// <returns>true表示执行成功,false表示执行失败</returns><BR> public bool GetID()<BR> {<BR> bool foo = false;<BR> <BR> DB db = new DB();</P>
<P> string strSql = "select max(menu_id) from menu";</P>
<P><BR> if (db.GetScalarByQuery(strSql, ref menu_id))<BR> {<BR> foo = true;<BR> }</P>
<P> return foo;<BR> }</P>
<P> public bool Insert2DB()<BR> {<BR> bool foo = false;</P>
<P> if (!GetID())<BR> {<BR> return foo;<BR> }</P>
<P> string strSql = "insert into menu ";<BR> string strAttribute = " (menu_id";<BR> string strValue = " (" + menu_id;</P>
<P> if (!menu_name.Equals(""))<BR> {<BR> strAttribute += " ,menu_name";<BR> strValue += ",'" + menu_name + "'";<BR> }</P>
<P> if (-1 != menu_parentID)<BR> {<BR> strAttribute += " ,menu_parentID";<BR> strValue += "," + menu_parentID;<BR> }</P>
<P> strAttribute += ")";<BR> strValue += ")";<BR> strSql += strAttribute + strValue;</P>
<P> DB db = new DB();</P>
<P> if (db.ExecCmdBySql(strSql))<BR> {<BR> foo = true;<BR> }</P>
<P> return foo;<BR> }</P>
<P> public bool Delete2DB()<BR> {<BR> bool foo = false;</P>
<P> string strSql = "delete from menu where menu_id = " + menu_id;</P>
<P> DB db = new DB();</P>
<P> if (db.ExecCmdBySql(strSql))<BR> {<BR> foo = true;<BR> }</P>
<P> return foo;<BR> }</P>
<P> public bool Delete2DB(string strConn)<BR> {<BR> bool foo = false;</P>
<P> string strSql = "delete from menu " + strConn;</P>
<P> DB db = new DB();</P>
<P> if (db.ExecCmdBySql(strSql))<BR> {<BR> foo = true;<BR> }</P>
<P> return foo;</P>
<P> }</P>
<P> public bool Update2DB()<BR> {<BR> bool foo = false;</P>
<P> string strSql = "update menu set menu_id = menu_id ";</P>
<P> if (!menu_name.Equals(""))<BR> {<BR> strSql += " menu_name = '" + menu_name + "'";<BR> }</P>
<P> if (-1 != menu_parentID)<BR> {<BR> strSql += " menu_parentID = " + menu_parentID;<BR> }</P>
<P> strSql += " where menu_id =" + menu_id ;</P>
<P> DB db = new DB();</P>
<P> if (db.ExecCmdBySql(strSql))<BR> {<BR> foo = true;<BR> }</P>
<P> return foo;<BR> }</P>
<P> public bool Query2DB(ref DataSet ds,string strColunm ,string strConn)<BR> {<BR> bool foo = false;</P>
<P> string strSql = "select * from ";</P>
<P> if (!strColunm.Equals(""))<BR> {<BR> strSql ="select " + strColunm + " from menu ";<BR> }</P>
<P> if (!strConn.Equals(""))<BR> {<BR> strSql += strConn;<BR> }</P>
<P> DB db = new DB();</P>
<P> if (db.GetDataSetByQuery(strSql, ref ds, "menu"))<BR> {<BR> foo = true;<BR> }</P>
<P> return foo;<BR> }</P>
<P>}</P></DIV>
回复:(爱像深蓝)手把手教你做下拉菜单[龙族首发][本...
<P>前台代码 Default.aspx<BR><BR></P><DIV class=htmlcode>
<P><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %></P>
<P><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" target="_blank" >http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</A>"></P>
<P><html xmlns="<a href="http://www.w3.org/1999/xhtml" target="_blank" >http://www.w3.org/1999/xhtml</A>" ><BR><head runat="server"><BR> <title>DropDownMenu</title><BR> <link href="css/base.css" rel="stylesheet" type="text/css" /><BR> <script type="text/javascript"><BR> var left = 0;<BR> var top = 0;<BR> function InitHtmlControl()<BR> {<BR> GetParentMenu();<BR> }<BR> <BR> function GetParentMenu()<BR> {<BR> _Default.GetMainMenu(CallBack_GetParentMenu);<BR> }<BR> <BR> function CallBack_GetParentMenu(res)<BR> {<BR> var strOut = res.value;<BR> <BR> document.getElementById("divTitleMenu").innerHTML = strOut;<BR> }<BR> <BR> function GetSubMenu(curCon,id)<BR> {<BR> var mybt = curCon.parentNode;<BR> <BR> left = mybt.offsetLeft; <BR> top = mybt.offsetTop;<BR> while(mybt = mybt.offsetParent) <BR> { <BR> top += mybt.offsetTop; <BR> left += mybt.offsetLeft; <BR> } </P>
<P> <BR> _Default.GetSubMenu(id,CallBack_GetSubMenu);<BR> }<BR> <BR> function CallBack_GetSubMenu(res)<BR> {<BR> var strOut = res.value;<BR> var div = document.getElementById("divSubMenu");<BR> div.style.left = left;<BR> div.style.top = top;<BR> div.innerHTML = strOut;<BR> }<BR> <BR> function Go2Conent(id)<BR> {//一般而言这里是为了传入一个编号得到一个相应的URL然后跳转,即"ContentMenu.aspx?menu_id="+id ; 这样的东东.这里没有写组织菜单的数据表,故只能向下向写这个的东东<BR> document.getElementById("divContent").innerHTML = "这里是菜单编号为" + id + "所代表的内容";<BR> }<BR> <BR> function ClearSubMenu()<BR> {<BR> document.getElementById("divSubMenu").innerHTML = "";<BR> }<BR> <BR> </script><BR></head><BR><body onload="InitHtmlControl()"><BR> <form id="frmDefault" runat="server"><BR> <div><BR> <table border="0" cellpadding="0" cellspacing="0" style="width: 770px; height: 600px"><BR> <tr><BR> <td style="width: 20px; height: 25px"><BR> </td><BR> <td style="width: 730px; height: 25px"><BR> <asp:Label ID="lblShowShowDRMenu" runat="server" Text="下拉菜单例"></asp:Label><BR> </td><BR> <td style="width: 20px; height: 25px"><BR> </td><BR> </tr><BR> <tr><BR> <td style="width: 20px; height: 25px"><BR> </td><BR> <td style="width: 730px; height: 25px"><BR> <div id="divTitleMenu"></div><BR> </td><BR> <td style="width: 20px; height: 25px"><BR> </td><BR> </tr><BR> <tr><BR> <td style="width: 20px; height: 25px"><BR> </td><BR> <td style="width: 730px; height: 25px"><BR> </td><BR> <td style="width: 20px; height: 25px"><BR> </td><BR> </tr><BR> <tr><BR> <td style="width: 20px; height: 500px"><BR> </td><BR> <td style="width: 730px; height: 500px;" onclick="ClearSubMenu()"><BR> <div id="divContent"></div><BR> </td><BR> <td style="width: 20px; height: 500px"><BR> </td><BR> </tr><BR> <tr><BR> <td style="width: 20px; height: 25px"><BR> </td><BR> <td style="width: 730px; height: 25px"><BR> </td><BR> <td style="width: 20px; height: 25px"><BR> </td><BR> </tr><BR> </table><BR> <div id="divSubMenu" style="position:absolute;left:200px;top:190px"></div><BR> <BR> </div><BR> </form><BR></body><BR></html></P></DIV>
<P><BR>页面处理类: default.aspx.cs<BR><BR></P>
<DIV class=htmlcode>
<P>using System;<BR>using System.Data;<BR>using System.Configuration;<BR>using System.Web;<BR>using System.Web.Security;<BR>using System.Web.UI;<BR>using System.Web.UI.WebControls;<BR>using System.Web.UI.WebControls.WebParts;<BR>using System.Web.UI.HtmlControls;<BR>using AjaxPro;<BR>public partial class _Default : System.Web.UI.Page <BR>{<BR> protected void Page_Load(object sender, EventArgs e)<BR> {<BR> AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));//注册ajax类<BR> GetMainMenu();<BR> }</P>
<P> /// <summary><BR> /// 得到主菜单(一级菜单)<BR> /// </summary><BR> /// <returns>菜单innerHTML形式</returns><BR> [AjaxPro.AjaxMethod]<BR> public string GetMainMenu()<BR> {<BR> string strOut = "";<BR> <BR> CMenu m = new CMenu();</P>
<P> string strConn = " where menu_parentID = 0";//父菜单标记为0的为一级菜单</P>
<P> DataSet ds = new DataSet();</P>
<P> if (m.Query2DB(ref ds, "menu_name,menu_id", strConn))<BR> {<BR> strOut += "<table style=\"width:100%;height:100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";<BR> strOut += "<tr class=\"tdCaleTitle\">";</P>
<P> int width = 0;<BR> <BR> foreach (DataRow row in ds.Tables[0].Rows)<BR> {<BR> strOut += "<td style=\"width:120px\">";<BR> strOut += "<a href=\"javascript:void(0);\" onmousemove=\"GetSubMenu(this," + row["menu_id"] + ")\">"; //调用前台js方法传入当前菜单的编号得到其所有的子菜单<BR> strOut += row["menu_name"];<BR> strOut += "</a>";<BR> strOut += "</td>";<BR> width += 120;<BR> }<BR> if (width > 770)<BR> {<BR> width = 0;<BR> }<BR> else<BR> {<BR> width = 770 - width;<BR> }<BR> strOut += "<td style=\"width:" + width + "px\"></td>";<BR> strOut += "</tr>";<BR> strOut += "</table>";<BR> }</P>
<P> return strOut;<BR> }</P>
<P> [AjaxPro.AjaxMethod]<BR> public string GetSubMenu(long id)<BR> {<BR> string strOut = "";</P>
<P> CMenu m = new CMenu();</P>
<P> string strConn = " where menu_parentID = " + id;//找个此父菜单下所有的子菜单</P>
<P> DataSet ds = new DataSet();</P>
<P> if (m.Query2DB(ref ds, "menu_name,menu_id", strConn))<BR> {<BR> strOut += "<table style=\"width:120px;height:25px\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";<BR> strOut += "<tr><td style=\"width:120px;height:25px\"></tr>"; //空行的目的是产生与父级菜单的交集<BR> int i = 0;<BR> foreach (DataRow row in ds.Tables[0].Rows)<BR> {<BR> string strCls = "tdCale_Red";<BR> if (0 == i % 2)<BR> {<BR> strCls = "tdCale";<BR> }<BR> strOut += "<tr class=\"" + strCls + "\">";<BR> strOut += "<td style=\"width:120px;height:25px\">";<BR> strOut += "<a href=\"javascript:void(0);\" onclick=\"Go2Conent(" + row["menu_id"] + ")\">"; //调用前台js方法传入当前菜单编号到达此菜单代表的页面<BR> strOut += row["menu_name"];<BR> strOut += "</a>";<BR> strOut += "</td>";<BR> strOut += "</tr>";<BR> i++;<BR> }<BR> <BR> strOut += "</table>";<BR> }</P>
<P> return strOut;<BR> }</P>
<P>}</P></DIV>
回复:(爱像深蓝)回复:(爱像深蓝)手把手教你做下...
<P>页面实现的效果截图:</P><P><BR>注:很多朋友问我半透明菜单如何制作,这里在下先说明做法,使用css的alpha滤镜可以,把opacity的值是透明度的百分比0为完全透明,100不透明.自己试着玩<BR>整个程序源码打包下载,大家可以看下:<BR></P><IMG src="http://home.goofar.com/shine_fzh/dropDownMenu02.jpg" border=0><BR><IMG src="http://home.goofar.com/shine_fzh/dropDownMenu03.jpg" border=0>[attach]29786[/attach]<BR> <P>我学习!不过web太差。。。</P>
页:
[1]
