DAL层
using System;
using System.Collections.Generic;
using System.Text;
using MODEL;
using System.Data;
namespace DAL
{
public static class DepartService
{
//查询父类
public static IList<Depart> GetDepart()
{
string sql = "select * from Depart";
return GetDepartInfosBySql(sql);
}
//根据父类id查询子类
public static IList<Branch> GetChild(int parentid)
{
string sql = "select * from Branch where departid = '" + parentid + "'";
return GetBranchInfosBySql(sql);
}
//执行父类sql语句
private static IList<Depart> GetDepartInfosBySql(string safeSql)
{
List<Depart> list = new List<Depart>();
try
{
DataTable table = DBHelper.GetDataSet(safeSql);
foreach (DataRow row in table.Rows)
{
Depart d = new Depart();
d.Id = (int)row["id"];
d.Name = (string)row["name"];
list.Add(d);
}
return list;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
}
//执行子类sql语句
private static IList<Branch> GetBranchInfosBySql(string safeSql)
{
List<Branch> list = new List<Branch>();
try
{
DataTable table = DBHelper.GetDataSet(safeSql);
foreach (DataRow row in table.Rows)
{
Branch b = new Branch();
b.Id = (int)row["id"];
b.Name = (string)row["name"];
b.Departid = (int)row["departid"];
list.Add(b);
}
return list;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}
}
}
}
BLL层
using System;
using System.Collections.Generic;
using System.Text;
using DAL;
using MODEL;
namespace BLL
{
public static class DepartManager
{
//查询父类
public static IList<Depart> GetDepart()
{
return DepartService.GetDepart();
}
//根据父类id查询子类
public static IList<Branch> GetChild(int parentid)
{
return DepartService.GetChild(parentid);
}
}
}
MODEL层
Branch.cs代码
using System;
using System.Collections.Generic;
using System.Text;
namespace MODEL
{
public class Branch
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int departid;
public int Departid
{
get { return departid; }
set { departid = value; }
}
}
}
Depart.cs代码
using System;
using System.Collections.Generic;
using System.Text;
namespace MODEL
{
public class Depart
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
}
Web层
Accordion.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Accordion.aspx.cs" Inherits="Accordion" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!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>Accordion控件实现菜单</title>
<style type="text/css">
.headerBg
{
background-color:#99CCFF; text-align:center; border:solid; border-width:1px; border-color:#CCCCCC
}
.contentBg
{
text-align:center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="margin:0 0 0 400px">
<div id="divColumn1" style="float:left; width:160px; height:98%;">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<cc1:Accordion ID="Accordion1" runat="server" HeaderCssClass="headerBg" ContentCssClass="contentBg">
</cc1:Accordion>
</div>
</div>
</form>
</body>
</html>
Accordion.aspx.cs代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using MODEL;
using AjaxControlToolkit;
public partial class Accordion : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Dislpay();
}
}
public void Dislpay()
{
IList<Depart> parent = DepartManager.GetDepart();
foreach (Depart parentNode in parent)
{
AccordionPane ap = new AccordionPane();
Label lblParentMenu = new Label();
//得到父节点id
int nodeId = parentNode.Id;
//得到父节点id的名字
lblParentMenu.Text = parentNode.Name;
//添加到标题部分
ap.HeaderContainer.Controls.Add(lblParentMenu);
IList<Branch> childNodes = DepartManager.GetChild(nodeId);
foreach (Branch childNode in childNodes)
{
Label lblChild = new Label();
lblChild.Text = childNode.Name+"<br>";
//添加到标题部分
ap.ContentContainer.Controls.Add(lblChild);
}
Accordion1.Panes.Add(ap);
}
}
}