Dynamic menu for .net

蓟浩旷
2023-12-01

 

c# code:   
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { this.lbl_Menu.Text = GetMenu("9999","999"); this.lbl_UserName.Text = "Tim"; this.lbl_MacIP.Text = Page.Request.UserHostAddress; } } private string GetMenu(string UserID, string TopMenuID) { System.Data.DataSet ds = GetMenuDataSet(UserID); System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("<ul class='topnav'>"); if (ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0) { System.Data.DataView dv = ds.Tables[0].DefaultView; dv.RowFilter = "MenuUppID='999'"; System.Data.DataTable dt = dv.ToTable(); for (int i = 0; i < dt.Rows.Count; i++) { System.Data.DataRow dr = dt.Rows[i]; sb.Append("<li><a href='" + (dr[2] != null ? dr[2].ToString() : "") + "'>" + (dr[1] != null ? dr[1].ToString() : "") + "</a>"); string subString = getSubMenu(dr[0].ToString(), dv); if (subString != "") { sb.Append(subString); } } } sb.Append("</ul>"); return sb.ToString(); } private string getSubMenu(string MenuUppID,System.Data.DataView dv) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); dv.RowFilter = "MenuUppID='" + MenuUppID + "'"; System.Data.DataTable dt = dv.ToTable(); if (dt.Rows.Count != 0) { sb.Append("<ul style=\"display: none;\" class=\"subnav\">"); } for (int i = 0; i < dt.Rows.Count; i++) { System.Data.DataRow dr = dt.Rows[i]; sb.Append("<li><a href='" + (dr[2] != null ? dr[2].ToString() : "") + "'>" + (dr[1] != null ? dr[1].ToString() : "") + "</a>"); } if (sb.Length != 0) { sb.Append("</ul>"); } return sb.ToString(); } private System.Data.DataSet GetMenuDataSet(string userID) { System.Data.DataSet ds = new System.Data.DataSet(); System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Add("ID"); dt.Columns.Add("MenuName"); dt.Columns.Add("MenuUrl"); dt.Columns.Add("MenuUppID"); dt.Columns.Add("MenuSort"); System.Data.DataRow dr = dt.NewRow(); dr[0] = "0"; dr[1] = "首页"; dr[2] = "index.htm"; dr[3] = "999"; dr[4] = "1"; dt.Rows.Add(dr); System.Data.DataRow dr1 = dt.NewRow(); dr1[0] = "1"; dr1[1] = "信息中心"; dr1[2] = "a.htm"; dr1[3] = "999"; dr1[4] = "1"; dt.Rows.Add(dr1); System.Data.DataRow dr2 = dt.NewRow(); dr2[0] = "2"; dr2[1] = "信息查询"; dr2[2] = "a1.htm"; dr2[3] = "1"; dr2[4] = "2"; dt.Rows.Add(dr2); System.Data.DataRow dr3 = dt.NewRow(); dr3[0] = "3"; dr3[1] = "信息修改"; dr3[2] = "a2.htm"; dr3[3] = "1"; dr3[4] = "3"; dt.Rows.Add(dr3); System.Data.DataRow dr4 = dt.NewRow(); dr4[0] = "4"; dr4[1] = "个人信息"; dr4[2] = "a3.htm"; dr4[3] = "999"; dr4[4] = "4"; dt.Rows.Add(dr4); System.Data.DataRow dr5 = dt.NewRow(); dr5[0] = "5"; dr5[1] = "网上"; dr5[2] = "a3.htm"; dr5[3] = "999"; dr5[4] = "5"; dt.Rows.Add(dr5); ds.Tables.Add(dt); return ds; }
    <style type="text/css">
        body {
	        margin: 0; padding: 0;
	        font: 10px normal Arial, Helvetica, sans-serif;
	        background: #ddd url(Images/body_bg.gif) repeat-x;
        }
        .container {
	        width: 960px;
	        margin: 0 auto;
	        position: relative;
        }
        ul.topnav li {
	        float: left;
	        margin: 0;	
	        padding: 0 15px 0 0;
	        position: relative; /*--Declare X and Y axis base--*/
	        
        }
        ul.topnav li a{
	        padding: 10px 5px;
	        color: #fff;
	        display: block;
	        text-decoration: none;
	        float: left;
        }
        ul.topnav li a:hover{
	        background: url(Images/topnav_hover.gif) no-repeat center top;
        }
        ul.topnav li span { /*--Drop down trigger styles--*/
	        width: 17px;
	        height: 35px;
	        float: left;
	        background: url(Images/subnav_btn.gif) no-repeat center top;
        }
        ul.topnav li span.subhover {background-position: center bottom; cursor: pointer;}
        ul.topnav li ul.subnav {
	        list-style: none;
	        position: absolute; /*--Important - Keeps subnav from affecting main navigation flow--*/
	        left: 0; top: 35px;
	        background: #333;
	        margin: 0; padding: 0;
	        display: none;
	        float: left;
	        width: 170px;
	        border: 1px solid #111;
        }
        ul.topnav li ul.subnav li{
	        margin: 0; padding: 0;
	        border-top: 1px solid #252525; /*--Create bevel effect--*/
	        border-bottom: 1px solid #444; /*--Create bevel effect--*/
	        clear: both;
	        width: 170px;
        }
        html ul.topnav li ul.subnav li a {
	        float: left;
	        width: 145px;
	        
	        padding-left: 20px;
        }
        html ul.topnav li ul.subnav li a:hover { /*--Hover effect for subnav links--*/
	        background: #222 url(Images/dropdown_linkbg.gif) no-repeat 10px center; 
        }
    </style>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
        $(document).ready(function () {
            $("ul.subnav").parent().append("<span></span>");
            $("ul.topnav li span").click(function () { 
                $(this).parent().find("ul.subnav").slideDown('fast').show();
                $(this).parent().hover(function () {
                }, function () {
                    $(this).parent().find("ul.subnav").slideUp('slow'); 
                });
            }).hover(function () {
                $(this).addClass("subhover"); 
            }, function () { 
                $(this).removeClass("subhover"); 
            });
        });
    </script>
<div class="container">
        <div id="topbanel" style="background: #ddd url(Images/body_bg.gif) repeat-x; height:102px">
            
        </div>

        <div id="header">
            <asp:Label runat="server" Text="" ID="lbl_Menu"></asp:Label>
        </div>
    </div>

    <div >
        <div>欢迎您!<asp:Label ID="lbl_UserName" runat="server"></asp:Label>   您的IP:<asp:Label ID="lbl_MacIP" runat="server"></asp:Label></div>
    </div>

 

 

转载于:https://www.cnblogs.com/Tim_Liu/archive/2010/11/22/1883990.html

 类似资料:

相关阅读

相关文章

相关问答