左边导航(一层级,二层级,三层级,四层级)
aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="left.aspx.cs" Inherits="DigitalCampus.Web.Page.left" %>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/left.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/icons.js"></script>
<script type="text/javascript" src="js/function.js"></script>
</head>
<body>
<form id="form1" runat="server">
<!-- 背景层DIV -->
<div class="div_documentbg" id="div_documentbg">
</div>
<script language="javascript" type="text/javascript">
ShowDocumentDivBG();
</script>
<div id="divMenu" class="left_menu" style="overflow: auto;">
<%=Leftmenu %>
</div>
</form>
<script language="javascript" type="text/javascript">
CloseTipsBG();
$(function () {
$(".menu_f").click(function () {
$("ul.s").slideUp(400)
if ($(this).next("ul.s").css("display") == "none") {
$(this).next("ul.s").slideDown(400);
}
else {
$(this).next("ul.s").slideUp(400);
}
});
if ($("ul.t")) {
$(".menu_s").click(function () {
$("ul.t").slideUp(400)
if ($(this).next("ul.t").css("display") == "none") {
$(this).next("ul.t").slideDown(400);
}
else {
$(this).next("ul.t").slideUp(400);
}
//$(this).next("ul.t").slideDown(400);
});
}
if ($("ul.ft")) {
$(".menu_t").click(function () {
$("ul.ft").slideUp(400)
if ($(this).next("ul.ft").css("display") == "none") {
$(this).next("ul.ft").slideDown(400);
}
else {
$(this).next("ul.ft").slideUp(400);
}
//$(this).next("ul.ft").slideDown(400);
});
}
})
</script>
</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;
using DigitalCampus.Common;
using DigitalCampus.Web.Common;
using DigitalCampus.DBUtility;
using System.Text;
namespace DigitalCampus.Web.Page
{
public partial class left : System.Web.UI.Page
{
protected int i = 0;
protected string Leftmenu = String.Empty;
protected string UserID = String.Empty;
protected string siteMap = String.Empty;
protected string showDivMenu = "1";
string Module_Id = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
CookiesMan.CheckAdminUserLogin();
UserID = CookiesMan.GetCookie("xy_admin_user", "userID");
Module_Id = "99020010000000000";
if (!IsPostBack)
{
LoadData();
}
siteMap = pfunction.QueryMenu(Module_Id);
}
protected void LoadData()
{
Leftmenu = GetUserLeftMenuByCache();
}
protected string GetUserLeftMenuByCache()
{
string cacheKey = "JSXY-GetUserLeftMenu-" + UserID;//注意命名规则 此处不能改变,因为清除缓存时用的到
object objCache = clsUtility.GetCache(cacheKey);
if (objCache == null)
{
#region 加载LeftMenu
DataTable dt = new DataTable();
StringBuilder Sbuild = new StringBuilder();
string strSql = string.Empty;
string userRole = string.Empty;
userRole = CookiesMan.GetCookie("xy_admin_user", "userRoleID");
if (string.IsNullOrEmpty(userRole)) userRole = "0";
strSql = @"select * FROM TB_MenuInfo where IsShow='1' and Menu_Code in (select Menu_ID FROM TB_UserForMenu where Role_ID in (" + userRole + ") ) order by Menu_Order,ID ";
dt = DbHelperSQL.Query(strSql).Tables[0];
if (dt.Rows.Count > 0)
{
string strUrl = String.Empty;//链接的URl
string strTag = String.Empty;//跳转的属性
string sty = string.Empty;
DataRow[] dtr1;
dtr1 = dt.Select(" Menu_Parent='0'", "Menu_Order");
Sbuild.Append(" <ul class=\"f\">\n");
#region 一级
for (int ii = 0; ii < dtr1.Length; ii++)
{
int parentCount = Convert.ToInt32(DbHelperSQL.GetSingle("select isnull(count(*),0) from TB_MenuInfo where Menu_Parent='" + dtr1[ii]["ID"] + "'").ToString());
if (parentCount != 0)
{
strUrl = "#";
strTag = "";
}
else
{
strUrl = dtr1[ii]["Menu_Url"].ToString();
strTag = "target=\"main\"";
}
Sbuild.Append("<li><a href=\"" + strUrl + "\" class=\"menu_f\" " + strTag + ">" + dtr1[ii]["Menu_Ico"] + dtr1[ii]["Menu_Name"] + "</a>\n");
#region 栏目二级
DataRow[] dtr2 = dt.Select(" Menu_Parent='" + dtr1[ii]["ID"] + "'", "Menu_Order");
if (dtr2.Length > 0) Sbuild.Append("<ul class=\"s\">\n");
for (int j = 0; j < dtr2.Length; j++)
{
string MenuParent = dtr2[j]["ID"].ToString();
int ParentID = Convert.ToInt32(DbHelperSQL.GetSingle("select isnull(count(Menu_Parent),0) from TB_MenuInfo where Menu_Parent='" + MenuParent + "'").ToString());
if (ParentID != 0)
{
strUrl = "#";
strTag = "";
}
else
{
strUrl = dtr2[j]["Menu_Url"].ToString();
strTag = "target=\"main\"";
}
Sbuild.Append("<li><a href=\"" + strUrl + "\" class=\"menu_s\" " + strTag + ">" + dtr2[j]["Menu_Ico"] + dtr2[j]["Menu_Name"] + "</a>\n");
#region 栏目三级
DataRow[] dtr3 = dt.Select(" Menu_Parent='" + dtr2[j]["ID"] + "'", "Menu_Order");
if (dtr3.Length > 0) Sbuild.Append("<ul class=\"t\">");
for (int jj = 0; jj < dtr3.Length; jj++)
{
string MenuParent3 = dtr3[jj]["ID"].ToString();
int ParentID3 = Convert.ToInt32(DbHelperSQL.GetSingle("select isnull(count(Menu_Parent),0) from TB_MenuInfo where Menu_Parent='" + MenuParent3 + "'").ToString());
if (ParentID3 != 0)
{
strUrl = "#";
strTag = "";
}
else
{
strUrl = dtr3[jj]["Menu_Url"].ToString();
strTag = "target=\"main\"";
}
Sbuild.Append("<li><a href=\"" + strUrl + "\" class=\"menu_t\" " + strTag + ">" + dtr3[jj]["Menu_Ico"] + dtr3[jj]["Menu_Name"] + "</a>\n");
#region 栏目四级
DataRow[] dtr4 = dt.Select(" Menu_Parent='" + dtr3[jj]["ID"] + "'", "Menu_Order");
if (dtr4.Length > 0) Sbuild.Append("<ul class=\"ft\">");
for (int jjj = 0; jjj < dtr4.Length; jjj++)
{
string MenuParent4 = dtr4[jjj]["ID"].ToString();
int ParentID4 = Convert.ToInt32(DbHelperSQL.GetSingle("select isnull(count(Menu_Parent),0) from TB_MenuInfo where Menu_Parent='" + MenuParent4 + "'").ToString());
if (ParentID4 != 0)
{
strUrl = "#";
strTag = "";
}
else
{
strUrl = dtr4[jjj]["Menu_Url"].ToString();
strTag = "target=\"main\"";
}
Sbuild.Append("<li><a href=\"" + strUrl + "\" " + strTag + ">" + dtr4[jjj]["Menu_Ico"] + dtr4[jjj]["Menu_Name"] + "</a></li>\n");
}
if (dtr4.Length > 0) Sbuild.Append(" </ul>\n");
#endregion
if (dtr4.Length == 0) Sbuild.Append(" </li>\n");
}
if (dtr3.Length > 0) Sbuild.Append(" </ul>\n");
#endregion
Sbuild.Append(" </li>\n");
}
if (dtr2.Length > 0) Sbuild.Append(" </ul>\n");
#endregion
Sbuild.Append("</li>\n");
}
#endregion
Sbuild.Append("</ul>\n");
//Leftmenu = Sbuild.ToString();
}
#endregion
objCache = Sbuild.ToString();
int minute = int.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["ModelCache"]);
clsUtility.AddCache(cacheKey, objCache, minute);
}
return objCache as string;
}
/// <summary>
/// 获取用户栏目 Cache
/// </summary>
/// <param name="UserID"></param>
/// <returns></returns>
public DataTable GetUserMenuDataByCache(string UserID)
{
string cacheKey = "JSXY-GetUserFunction-" + UserID;//注意命名规则 此处不能改变,因为清除缓存时用的到
object objCache = clsUtility.GetCache(cacheKey);
if (objCache == null)
{
try
{
string strSql = string.Empty;
string userRole = string.Empty;
userRole = CookiesMan.GetCookie("xy_admin_user", "userRoleID");
if (string.IsNullOrEmpty(userRole)) userRole = "0";
strSql = @"select * FROM TB_MenuInfo where IsShow='1' and Menu_Code in (select Menu_ID FROM TB_UserForMenu where Role_ID in (" + userRole + ") ) Order By Menu_Order,ID ";
DataTable dt = new DataTable();
dt = DbHelperSQL.Query(strSql).Tables[0];
objCache = dt;
int minute = int.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["ModelCache"]);
clsUtility.AddCache(cacheKey, objCache, minute);
}
catch
{ }
}
return objCache as DataTable;
}
/// <summary>
/// 加载栏目
/// </summary>
private void DataTree()
{
DataTable dt = new DataTable();
StringBuilder Sbuild = new StringBuilder();
//string strSql = string.Empty;
//string userRole = string.Empty;
//userRole = CookiesMan.GetCookie("xy_admin_user", "userRoleID");
//if (string.IsNullOrEmpty(userRole)) userRole = "0";
//strSql = @"select * FROM TB_MenuInfo where IsShow='1' and Menu_Code in (select Menu_ID FROM TB_UserForMenu where Role_ID in (" + userRole + ") ) order by Menu_Order,ID ";
//dt = DbHelperSQL.Query(strSql).Tables[0];
dt = GetUserMenuDataByCache(UserID);
if (dt.Rows.Count == 0)
{
return;
}
else
{
string strUrl = String.Empty;//链接的URl
string strTag = String.Empty;//跳转的属性
string sty = string.Empty;
DataRow[] dtr1;
dtr1 = dt.Select(" Menu_Parent='0'", "Menu_Order");
Sbuild.Append(" <ul class=\"f\">\n");
for (int ii = 0; ii < dtr1.Length; ii++)
{
int parentCount = Convert.ToInt32(DbHelperSQL.GetSingle("select isnull(count(*),0) from TB_MenuInfo where Menu_Parent='" + dtr1[ii]["ID"] + "'").ToString());
if (parentCount != 0)
{
strUrl = "#";
strTag = "";
}
else
{
strUrl = dtr1[ii]["Menu_Url"].ToString();
strTag = "target=\"main\"";
}
Sbuild.Append("<li><a href=\"" + strUrl + "\" class=\"menu_f\" " + strTag + ">" + dtr1[ii]["Menu_Ico"] + dtr1[ii]["Menu_Name"] + "</a>\n");
#region 栏目二级
DataRow[] dtr2 = dt.Select(" Menu_Parent='" + dtr1[ii]["ID"] + "'", "Menu_Order");
if (dtr2.Length > 0) Sbuild.Append("<ul class=\"s\">\n");
for (int j = 0; j < dtr2.Length; j++)
{
string MenuParent = dtr2[j]["ID"].ToString();
int ParentID = Convert.ToInt32(DbHelperSQL.GetSingle("select isnull(count(Menu_Parent),0) from TB_MenuInfo where Menu_Parent='" + MenuParent + "'").ToString());
if (ParentID != 0)
{
strUrl = "#";
strTag = "";
}
else
{
strUrl = dtr2[j]["Menu_Url"].ToString();
strTag = "target=\"main\"";
}
Sbuild.Append("<li><a href=\"" + strUrl + "\" class=\"menu_s\" " + strTag + ">" + dtr2[j]["Menu_Ico"] + dtr2[j]["Menu_Name"] + "</a>\n");
#region 栏目三级
DataRow[] dtr3 = dt.Select(" Menu_Parent='" + dtr2[j]["ID"] + "'", "Menu_Order");
if (dtr3.Length > 0) Sbuild.Append("<ul class=\"t\">");
for (int jj = 0; jj < dtr3.Length; jj++)
{
string MenuParent3 = dtr3[jj]["ID"].ToString();
int ParentID3 = Convert.ToInt32(DbHelperSQL.GetSingle("select isnull(count(Menu_Parent),0) from TB_MenuInfo where Menu_Parent='" + MenuParent3 + "'").ToString());
if (ParentID3 != 0)
{
strUrl = "#";
strTag = "";
}
else
{
strUrl = dtr3[jj]["Menu_Url"].ToString();
strTag = "target=\"main\"";
}
Sbuild.Append("<li><a href=\"" + strUrl + "\" class=\"menu_t\" " + strTag + ">" + dtr3[jj]["Menu_Ico"] + dtr3[jj]["Menu_Name"] + "</a>\n");
#region 栏目四级
DataRow[] dtr4 = dt.Select(" Menu_Parent='" + dtr3[jj]["ID"] + "'", "Menu_Order");
if (dtr4.Length > 0) Sbuild.Append("<ul class=\"ft\">");
for (int jjj = 0; jjj < dtr4.Length; jjj++)
{
string MenuParent4 = dtr4[jjj]["ID"].ToString();
int ParentID4 = Convert.ToInt32(DbHelperSQL.GetSingle("select isnull(count(Menu_Parent),0) from TB_MenuInfo where Menu_Parent='" + MenuParent4 + "'").ToString());
if (ParentID4 != 0)
{
strUrl = "#";
strTag = "";
}
else
{
strUrl = dtr4[jjj]["Menu_Url"].ToString();
strTag = "target=\"main\"";
}
Sbuild.Append("<li><a href=\"" + strUrl + "\" " + strTag + ">" + dtr4[jjj]["Menu_Ico"] + dtr4[jjj]["Menu_Name"] + "</a></li>\n");
}
if (dtr4.Length > 0) Sbuild.Append(" </ul>\n");
#endregion
if (dtr4.Length == 0) Sbuild.Append(" </li>\n");
}
if (dtr3.Length > 0) Sbuild.Append(" </ul>\n");
#endregion
Sbuild.Append(" </li>\n");
}
if (dtr2.Length > 0) Sbuild.Append(" </ul>\n");
#endregion
Sbuild.Append("</li>\n");
}
Sbuild.Append("</ul>\n");
Leftmenu = Sbuild.ToString();
}
}
}
}
表的结构:
TB_MenuInfo: TB_UserForMenu:
ID bigint 自动编号 ID bigint 自动编号
Menu_Code nvarchar(50) 编码 Role_ID int 角色ID
Menu_Name nvarchar(50) 名称 Menu_ID bigint 栏目ID
Menu_Parent nvarchar(50) 父级 Menu_Name nvarchar(50) 栏目名称
Menu_Rating nvarchar(50) 层级 Fun_ID int 操作项ID
Menu_Url nvarchar(1000) Url Fun_Name nvarchar(50) 操作项名称
Menu_Ico nvarchar(200) Ico CUser nvarchar(50) 添加人
Menu_Order int 排序 CTime datetime 添加时间
IsShow nvarchar(50) 是否显示 UUser nvarchar(50) 修改人
IsLast nvarchar(50) 是否显示操作项 UTime datetime 修改时间
Note nvarchar(1000) 备注
CUser nvarchar(50) 创建人
CTime datetime 创建时间
UUser nvarchar(50) 修改人
UTime datetime 修改时间
OA项目之左导航
最新推荐文章于 2024-08-23 14:03:47 发布