zoukankan      html  css  js  c++  java
  • 页面根据XML里权限动态生成菜单

    1.XML文件,分为大标题与小标题,文档中层级很清楚,不需再多解释

    <?xml version="1.0" encoding="utf-8" ?>
    <doc>
    <NavMenu>
    <Menus text="商铺管理" imgUrl="../images/main_02.jpg" ID="1,2">
    <MenuItem ID="1" Text="商铺列表页面" href="/shop/getshoplist.html" target="main" onclick="replay(this)"></MenuItem >
    <MenuItem ID="2" Text="添加商铺页面" href="/shop/Addshop.html" target="main" onclick="replay(this)"></MenuItem >
    </Menus>

    <Menus text="品牌管理" imgUrl="../images/main_02.jpg" ID="3,4">
    <MenuItem ID="3" Text="品牌列表页面" href="/shop/GetbrandList.html" target="main" onclick="replay(this)"></MenuItem >
    <MenuItem ID="4" Text="添加品牌页面" href="/shop/Addbrand.html" target="main" onclick="replay(this)"></MenuItem >
    </Menus>

    <Menus text="套餐管理" imgUrl="../images/main_02.jpg" ID="5,6">
    <MenuItem ID="5" Text="套餐列表页面" href="/shop/GetpackageList.html" target="main" onclick="replay(this)"></MenuItem >
    <MenuItem ID="6" Text="添加套餐页面" href="/shop/Addpackage.html" target="main" onclick="replay(this)"></MenuItem >
    </Menus>

    <Menus text="会员管理" imgUrl="../images/main_02.jpg" ID="7">
    <MenuItem ID="7" Text="会员列表页面" href="/member/GetmemberList.html" target="main" onclick="replay(this)"></MenuItem >
    </Menus>
    </NavMenu>
    </doc>

    2.MVC的View页面,<%%>里的C#代码可以直接应用于一般处理程序或BLL层或者Controllers中。

    此段代码的主要思想是将存在XML里的标题权限ID是否存在于Session里的泛型集合(从数据库查出全权限的DataSet),若有则显示该标题(大标题ID是从属于它的小标题ID通过逗号拼接的字符串)。

    <%string xmlfile = Server.MapPath("") + "\\XML\\Menu.xml"; //Server.MapPath(“”) 返回当前页面的物理路径;” \XML\\Menu.xml”, 和前边的字符串合起来为XML文档所在的路径
    System.Xml.XmlTextReader reader
    = new System.Xml.XmlTextReader(xmlfile);//XML文档读取器声明
    List
    <System.Data.DataTable> listTb = new List<System.Data.DataTable>();//声明新的泛型
    var mobel
    = (命名空间.Model.Admin_UserModel)Session["txtuname"];//将Session取出类型转换为实体类
    listTb
    = mobel.AU_list;////将Session中的泛型(也是DataTable类型)取出s
    while (reader.Read())
    {
    %>
    <% if (reader.NodeType == System.Xml.XmlNodeType.Element)
    {
    if (reader.Name == "Menus") //对应XML里的大标题
    {
    string str = reader["ID"].ToString(); //XML里的大标题ID
    string[] strArr = str.Split(',');
    //lamda表达式返回 tr(若XML里的节点是否存在于Session里的泛型,则返回泛型集合)
    List
    <System.Data.DataTable> Tr = listTb.FindAll(delegate(System.Data.DataTable t) {
    foreach (System.Data.DataRow dr in t.Rows)
    {
    if (strArr.Length > 1)
    {
    if (strArr[0] == dr["AUP_PermissionID"].ToString() || strArr[1] == dr["AUP_PermissionID"].ToString())
    {
    return
    true;
    }
    }
    else{
    if (strArr[0] == dr["AUP_PermissionID"].ToString())
    {
    return
    true;
    }
    }
    }
    return
    false;
    });

    if (Tr.Count!=0)
    {
    %>
    <div class="nav1">
    <img src="<%=reader["imgUrl"].ToString() %>" /><%=reader["text"].ToString()%></div>
    <%
    }
    }
    %>
    <div class="foter">
    <ul>
    <%if (reader.Name == "MenuItem") //对应XML里的小标题(操作与大标题相似)
    {

    List
    <System.Data.DataTable> Tr = listTb.FindAll(delegate(System.Data.DataTable t)
    {
    foreach (System.Data.DataRow dr in t.Rows)
    {
    if (reader["ID"] == dr["AUP_PermissionID"].ToString())
    {
    return
    true;
    }
    }
    return
    false;
    });


    if (Tr.Count!=0l)
    {
    %>
    <li><a href="<%=reader["href"].ToString() %>" target="main" onclick="replay(this)">
    <%=reader["Text"].ToString()%></a></li>
    <%}
    }
    %>
    </ul>
    </div>
    <%} %>
    <%} %>


    就这些,有些麻烦但是很容易理解,难一点的也许就是/lamda表达式那块



  • 相关阅读:
    暑假第二十七测
    暑假第二十七测
    【真题解】牛宫
    【伪题解】牛宫
    最优贸易
    跳马问题
    求和问题
    【题解】山区建小学
    OpenStack之虚机冷迁移代码简析
    OpenStack之虚机热迁移代码解析
  • 原文地址:https://www.cnblogs.com/ereryday/p/2367030.html
Copyright © 2011-2022 走看看