asp.net二级菜单导航 实例

.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default17.aspx.cs" Inherits="Default17" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-1.8.3.js" type="text/javascript"></script>
    <script type ="text/javascript">
        $(document).ready(function () {
            $("a.m0").mouseenter(function () {
                $(".submenu").hide();//开启之前全部关闭子菜单
                $("+.submenu", this).fadeIn(200);
            });

            $("#div_menu").mouseleave(function () {
                $(".submenu").fadeOut(200);
            });
        });
    </script>


    <style type ="text/css" >
        body { margin:0px; padding:0px;}
        #div_menu { border: 1px solid silver;
                    background-image:url(imgs/back_head.jpg);
                    background-repeat:repeat-x;  }
        
        a { display:block; text-decoration:none; color:Gray;}
        a:hover { color:#930;} 
        a.m0 { margin:10px 0px 10px 0px;
               padding:0px 10px 0px 10px; }
        .menuset+.menuset a.m0
        { border-left:1px solid silver;}
        a.m1 { margin:5px; padding:10px;
               font-size:12px; }
        
        .menuset { float:left;}
        .submenu { position:absolute;
                   display:none; }
        
        
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        
        <div id="div_menu" runat="server"></div>
    </div>
    </form>
</body>
</html>
.aspx.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class Default17 : System.Web.UI.Page
{   string str_cnn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=";
    string str_sourcefile = "mdb\\mydb.mdb";
    OleDbConnection cnn;
    OleDbCommand cmd;
    OleDbDataReader datar;
    string str_sql;

    protected void Page_Load(object sender, EventArgs e)
    {
        makenav();
    }


    protected void makenav() { 
        string str_conn = str_cnn + MapPath(str_sourcefile);
        cnn = new OleDbConnection(str_conn);
        cnn.Open();

        str_sql = "SELECT t_menuL0.*, t_menuL1.* FROM t_menuL1 RIGHT JOIN t_menuL0 ON t_menuL1.m1_m0id = t_menuL0.m0_id";
        cmd = new OleDbCommand(str_sql, cnn);
        datar = cmd.ExecuteReader();

        Panel pnl_menuset, pnl_submenu;
        HyperLink a_m0, a_m1;

        while (datar.Read()) {
            string s_m0 = datar["m0_id"].ToString();


            pnl_menuset = (Panel)div_menu.FindControl("div_menuset_"+s_m0);
            if (pnl_menuset == null) {
                pnl_menuset = new Panel(); pnl_menuset.CssClass = "menuset";
                pnl_menuset.ID = "div_menuset_" + s_m0;
                div_menu.Controls.Add(pnl_menuset);
            }
            pnl_menuset = (Panel)div_menu.FindControl("div_menuset_" + s_m0);

            a_m0 = (HyperLink)FindControl("a_m0_"+s_m0);
            if (a_m0 == null) {
                a_m0 = new HyperLink(); a_m0.CssClass = "m0";
                a_m0.ID = "a_m0_" + s_m0;
                a_m0.Text = datar["m0_ttl"].ToString();
                a_m0.NavigateUrl = datar["m0_url"].ToString();
                pnl_menuset.Controls.Add(a_m0);
            }

            if (!(datar["m1_ttl"] is DBNull)) {
                pnl_submenu = (Panel)FindControl("div_submenu_" + s_m0);
                if (pnl_submenu == null) {
                    pnl_submenu = new Panel(); pnl_submenu.CssClass = "submenu";
                    pnl_submenu.ID = "div_submenu_" + s_m0;
                    pnl_menuset.Controls.Add(pnl_submenu);
                }
                pnl_submenu = (Panel)FindControl("div_submenu_" + s_m0);


                a_m1 = new HyperLink(); a_m1.CssClass = "m1";
                a_m1.ID = "a_m1_" + datar["m1_id"].ToString();
                a_m1.Text = datar["m1_ttl"].ToString();
                a_m1.NavigateUrl = datar["m1_url"].ToString();
                pnl_submenu.Controls.Add(a_m1);


            
            }

        }

        cnn.Close();

        Panel p = new Panel();
        p.Style.Add("clear", "both");
        div_menu.Controls.Add(p);
    }
}

效果如下:



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。