两个Repeater嵌套实现动态菜单(ado.net+sql和xml+Linq两种读取数据方式)

今天在实现一个增强web项目可扩展性的一个功能,把以前的静态菜单改成了动态的扩展、可维护的菜单

1、菜单存储在数据表中

<asp:Repeater ID="Repeater1" runat="server" 
onitemdatabound
="Repeater1_ItemDataBound">
<ItemTemplate>
<h1 class="type"><a href="javascript:void(0)"><%# DataBinder.Eval(Container.DataItem, "MT_name")%></a></h1>
<asp:Repeater ID="Repeater2" runat="server">
<HeaderTemplate>
<div class="content">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="images/menu_topline.gif" width="182" height="5"/></td>
</tr>
</table>
<ul class="MM">
</HeaderTemplate>
<ItemTemplate>
<li><a href="<%# DataBinder.Eval(Container.DataItem, "MI_page")%>" target="main"><%# DataBinder.Eval(Container.DataItem, "MI_name")%></a></li>
</ItemTemplate>
<FooterTemplate>
</ul></div>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>

 服务器端代码

publicpartialclass left : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sql ="select MT_ID,MT_name from ModuleTopInfo";
SqlDbHelper db =new SqlDbHelper();//SqlHelper类你懂得
Repeater1.DataSource =db.ExecuteDataTable(sql);
Repeater1.DataBind();
}
}

protectedvoid Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string id = ((DataRowView)e.Item.DataItem).Row["MT_ID"].ToString();
Repeater rpt
= (Repeater)e.Item.FindControl("Repeater2");
string sql ="select MI_ID,MI_page,MI_name from ModuleInfo where MT_ID=@id";
SqlParameter[] parameters
=new SqlParameter[] {
new SqlParameter("@id",id)
};
SqlDbHelper db
=new SqlDbHelper();
DataTable dt
=new DataTable();
dt
= db.ExecuteDataTable(sql,CommandType.Text,parameters);
if (rpt !=null)
{
rpt.DataSource
= dt;
rpt.DataBind();
}
}
}

 实现效果截图

2、菜单在xml文件

xml

View Code
<?xml version="1.0" encoding="utf-8" ?>
<AdminMenu>
<Menus Text="内容管理" id="1">
<MenuItem Text="中心简介维护" url="#" id="1" />
<MenuItem Text="科研方向维护" url="#" id="1" />
<MenuItem Text="荣获奖励维护" url="#" id="1" />
<MenuItem Text="技术文章维护" url="#" id="1" />
<MenuItem Text="实训项目维护" url="#" id="1" />
<MenuItem Text="规章制度维护" url="#" id="1" />
<MenuItem Text="首页简介维护" url="#" id="1" />
</Menus>
<Menus Text="学生报名" id="2">
<MenuItem Text="开通报名系统" url="#" id="2" />
<MenuItem Text="学生信息查询" url="#" id="2"/>
<MenuItem Text="学生信息维护" url="#" id="2" />
<MenuItem Text="添加通过学生" url="#" id="2"/>
</Menus>
<Menus Text="教师维护" id="3">
<MenuItem Text="开通教师账号" url="#" id="3"/>
<MenuItem Text="教师信息维护" url="#" id="3" />
</Menus>
<Menus Text="图片维护" id="4">
<MenuItem Text="上传图片" url="#" id="4" />
<MenuItem Text="图片管理" url="#" id="4" />
</Menus>
<Menus Text="个人资料" id="5">
<MenuItem Text="修改密码" url="#" id="5"/>
</Menus>
</AdminMenu>

页面主要代码

View Code
<asp:Repeater ID="Repeater1" runat="server" 
onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<tr>
<td class="menu_first_img mouse" id="td<%# DataBinder.Eval(Container.DataItem, "id") %>" onClick="show(<%# DataBinder.Eval(Container.DataItem, "id") %>)"><div class="menu" ><a href="#" style=" color:White; text-decoration:none">
<%# DataBinder.Eval(Container.DataItem, "Text")%>
</a> </div></td>
</tr>
<tr>
<td id="show<%# DataBinder.Eval(Container.DataItem, "id") %>" style=" display:none;" ><table width="100%" border="0" cellspacing="0" cellpadding="0">
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<tr>
<td class="left"><table width="100%" height="24" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="40" align="center"><img src="../images/5.png" alt="1" width="16" height="16"></td>
<td><div class="menu1"><a href="<%# DataBinder.Eval(Container.DataItem, "url")%>" style=" color:White; text-decoration:none">
<%# DataBinder.Eval(Container.DataItem, "menuText")%>
</a> </div></td>
</tr>
</table></td>
</tr>
</ItemTemplate>
</asp:Repeater></table></td></tr>
</ItemTemplate>
</asp:Repeater>

后台代码

  protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string path = Server.MapPath("~/Manger/MenuXML/admin.xml");
XElement root = XElement.Load(path);
var result = from menu in root.Elements("Menus")
select new
{
Text = menu.Attribute("Text").Value,
ID = menu.Attribute("id").Value
};
Repeater1.DataSource = result;
Repeater1.DataBind();
}

}

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string id = DataBinder.Eval(e.Item.DataItem, "id").ToString();
Repeater rpt = (Repeater)e.Item.FindControl("Repeater2");
string path = Server.MapPath("~/Manger/MenuXML/admin.xml");
XElement root = XElement.Load(path);
var result = from menu in root.Descendants("MenuItem")
where menu.Attribute("id").Value.Equals(id)
select new
{
menuText = menu.Attribute("Text").Value,
url = menu.Attribute("url").Value,
curid = menu.Attribute("id").Value
};
if (rpt != null)
{
rpt.DataSource = result;
rpt.DataBind();
}
}
}

实现截图




posted @ 2011-07-25 17:58  wangyan9110  阅读(1480)  评论(0编辑  收藏  举报