注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

一车,一房,一个温暖家

10万左右的车,80平方的房子,这是我奋斗的暖暖的家

 
 
 

日志

 
 

引用 C# 封装 树结构   

2015-06-16 18:50:07|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

[assembly: System.Web.UI.WebResource("CurrentRequestIP.LeftTreeView.Resources.LeftTreeScript.js", "text/javascript")]

namespace CurrentRequestIP.LeftTreeView
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:LeftTreeView runat=server></{0}:LeftTreeView>")]
    public class LeftTreeView : Table
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string XMLPath
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("200")]
        [Localizable(true)]
        public string TableWidth
        {
            get
            {
                return ViewState["TableWidth"] + string.Empty;
            }
            set
            {
                ViewState["TableWidth"] = value;
            }
        }

        public Table ShowCurrent
        {
            get;
            set;
        }

       

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

 

            // Define the resource name and type.
            String rsname = "CurrentRequestIP.LeftTreeView.Resources.LeftTreeScript.js";

            //this.Page.ClientScript.RegisterClientScriptResource(this.GetType(), rsname);
            // 注册所需脚本
            if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "yy_stv_scriptLibrary"))
            {
                this.Page.ClientScript.RegisterClientScriptInclude
                (
                    this.GetType(),
                    "yy_stv_scriptLibrary",
                    this.Page.ClientScript.GetWebResourceUrl
                    (

                        this.GetType(), rsname

                    )
                );
            }

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                this.CreateTree();
            }catch
            {
            }
        }

        protected override void  RenderContents(HtmlTextWriter writer)
        {
           base.RenderContents(writer);
        }

        #region 生成功能菜单
        /// <summary>
        /// 生成功能菜单
        /// </summary>
        public void CreateTree()
        {
           
            this.Attributes.Add("cellpadding", "0");
            this.Attributes.Add("cellspacing", "0");
           
            //设置鼠标样式
            this.Style.Add("cursor", "hand");

            this.CssClass = "tableMenuStyle";

            //记录菜单分组项的个数
            int intCount = 0;
            XmlDataDocument xmlDoc = new XmlDataDocument();
            string strXmlFilePath = HttpContext.Current.Server.MapPath(this.XMLPath);

           
            //获取菜单xml文件
            xmlDoc.Load(strXmlFilePath);
            XmlNode node = xmlDoc.SelectSingleNode("Tree");
            //获取菜单分组项
            XmlNodeList list = node.SelectNodes("node");
            TableCell tcell = null;
            foreach (XmlNode cNode in list)
            {
                //一级菜单
                TableRow row = new TableRow();
                TableCell cell = new TableCell();
               
                cell.Attributes.Add("class", "FirstFont");
                cell.Attributes.Add("width", this.TableWidth);
                cell.Attributes.Add("height", "30");
                cell.Attributes.Add("url", cNode.Attributes["url"].Value.Trim());
                cell.Text = "<b>" + cNode.Attributes["name"].Value + "</b>";
                cell.ToolTip = cNode.Attributes["tooltip"].Value;
                row.Cells.Add(cell);
                this.Rows.Add(row);

                //获取菜单的功能项
                XmlNodeList cList = cNode.ChildNodes;
                if (cList.Count != 0)
                {
                    row = new TableRow();
                    row.Attributes.Add("item", "menu");

                    TableCell oCell = new TableCell();
                    //添加菜单功能项
                    Table table = SetTable(cNode.Attributes["id"].Value, cList);
                    Panel pnl = new Panel();
                    pnl.Style.Add("overflow", "auto");
                    pnl.Style.Add("height", "100%");
                    pnl.Style.Add("width", this.TableWidth);
                    pnl.Controls.Add(table);
                    oCell.Controls.Add(pnl);
                    oCell.Attributes.Add("valign", "top");
                    oCell.Attributes.Add("width", this.TableWidth);
                    oCell.Attributes.Add("class", "childTable");
                    row.Cells.Add(oCell);
                    this.Rows.Add(row);
                    //为单元格添加事件,改变菜单是否显示(注:事件只能在此处添加,因为table在没有加入到rows里时,id并不是在客户端显示的ID)
                    cell.Attributes.Add("onclick", "change(this,'" + table.ClientID + "','" + oCell.ClientID + "','"+this.ClientID+"');");

                    //默认为第一项是显示菜单
                    if (intCount == 0)
                    {
                        //table.Style.Remove("display");
                        tcell = oCell;
                        this.ShowCurrent = table;
                    }
                    intCount++;
                }
            }

            if (this.ShowCurrent != null)
            {
                this.ShowCurrent.Style.Remove("display");
            }
           
        }
        #endregion

        #region 设置子菜单项
        /// <summary>
        /// 设置子菜单项
        /// </summary>
        /// <param name="strID">控件ID</param>
        /// <param name="list">菜单项列表</param>
        /// <returns></returns>
        public Table SetTable(string strID, XmlNodeList list)
        {
            Table table = new Table();
           
            table.Attributes.Add("heigh", "100%");
            table.Attributes.Add("border", "0");
            table.Attributes.Add("cellpadding", "0");
            table.Attributes.Add("cellspacing", "0");

            //默认为隐藏菜单
            table.Style.Add("display", "none");
            //设置table ID,此ID应唯一,因为在客户端要通过此ID来判断是否显示此菜单
            table.ID = strID;
            table.Style.Add("width", this.TableWidth);
            foreach (XmlNode node in list)
            {
                TableRow tr = new TableRow();
                TableCell cell = new TableCell();
                cell.Attributes.Add("valign", "middle");
                cell.Attributes.Add("width", this.TableWidth);
                //二级菜单样式
                cell.Attributes.Add("class", "SecondFont");
                cell.Attributes.Add("onmouseover", "if(this.className!='MouseDown') this.className='MouseMove'");
                cell.Attributes.Add("onmouseout", "if(this.className!='MouseDown') this.className='SecondFont'");
                cell.ID = node.Attributes["id"].Value;
                //设置菜单的高度
                cell.Height = 25;

                //菜单子项加两个空格
                cell.Text = node.Attributes["name"].Value;
                //为菜单添加要跳转的URL
                cell.Attributes.Add("onclick", "Redirect(this,'" + node.Attributes["url"].Value + "');");
                cell.ToolTip = node.Attributes["tooltip"].Value;
                tr.Cells.Add(cell);
                table.Rows.Add(tr);

                if (this.Page.Request.Url.ToString().Contains(node.Attributes["url"].Value))
                {
                    this.ShowCurrent = table;
                }
            }
      
            return table;
        }
        #endregion


      
    }
}

 


//单击对象,要操作的表格,菜单表格


function change(objImg,objID,objTD,txtTree)

   
    //获取功能菜单所有的行
   
    var Table=document.getElementById(txtTree).rows;
 
    for(var i=0;i<Table.length;i++)
    {
        //查找行中是否包括table,如果有则功能菜单,否则为菜单分组名称
       
        var oTable=Table[i].getElementsByTagName("table");
        if(oTable.length==0)
        {
           continue;
        }
        //如果表格不是要操作的表格则隐藏
       
       
        if(oTable[0].id!=objID)
        {
            oTable[0].style.display="none";
          
        }
        //如果要操作的表格为显示状态,则隐藏
       
       
        else if(oTable[0].style.display=="")
        {
            oTable[0].style.display="none";
          
        }
        //如果要操作的表格为隐藏则显示
       
       
        else
        {
            oTable[0].style.display="";
           
            if(objImg.url!="" && objImg.url!=null)
            {
                Redirect(oTable[0],objImg.url);
            }
        }
       
    }
}

//设置iframe地址


function Redirect(obj,objUrl)
{
    //window.open(objUrl);
    window.location.href=objUrl;
}

<?xml version="1.0" encoding="utf-8" ?>
<Tree>
  <node id="danweibuzhu" guid=""  name="单位补助补贴发放" url="" tooltip="单位补助补贴发放">
    <node id="htyggzd" guid=""  name="合同用工工资单" url="/Office/ContractPayForm/" tooltip="合同用工工资单申请"></node>
    <node id="htyggzdzh" guid=""  name="合同用工工资单综合部" url="/Office/ContractPayMerge/" tooltip="合同用工工资单综合部审批"></node>
    <node id="qgzxbz" guid="" name="勤工助学补助" url="/Office/WorkStudyAllowance/" tooltip="勤工助学补助申请"></node>
    <node id="qgzxbzzh" guid="" name="勤工助学补助综合部" url="/Office/WorkStudySynthesis/" tooltip="勤工助学补助申请综合部"></node>
    <node id="htygbx" guid="" name="合同用工保险" url="/Office/ContractWageMag/" tooltip="合同用工保险综合部"></node>
    <node id="htygbxzh" guid="" name="合同用工保险综合部" url="/Office/ContractWageMerge/" tooltip="合同用工保险综合部审批"></node>
    <node id="htgjjrjbbz" guid="" name="合同工节假日加班补助" url="/Office/ContractHolidayAllowance/" tooltip="合同工节假日加班补助审批"></node>
    <node id="htgjjrjbbzzh" guid="" name="合同工节假日加班补助综合部" url="/Office/ContractHolidaySynthesis/" tooltip="合同工节假日加班补助综合部"></node>
    <node id="zzzgjjrjbbz" guid="" name="在职职工节假日加班补助" url="/Office/EmployeeHolidayAllowance/" tooltip="在职职工节假日加班补助"></node>
    <node id="zzzgjjrjbbzzh" guid="" name="在职职工节假日加班补助综合部" url="/Office/EmployeeHolidaySynthesis/" tooltip="在职职工节假日加班补助综合部"></node>
  </node>
  <node id="kucunshebeiguanli" guid=""  name="库存设备管理系统" url="" tooltip="库存设备管理系统">
    <node id="caigoudan" guid=""  name="采购单" url="/Office/Lists/Warehousing/" tooltip="采购单填写"></node>
    <node id="rukubiao" guid="" name="入库表" url="/Office/Lists/RepertoryDetails/" tooltip="入库表"></node>
    <node id="chukubiao" guid="" name="出库表" url="/Office/Lists/Exwarehouse/" tooltip="出库表"></node>
    <node id="wupinlingyong" guid="" name="物品领用" url="/Office/Lists/ProductApply/" tooltip="物品领用"></node>
    <node id="shebeijieyong" guid="" name="设备借用" url="/Office/Lists/EquipmentBorrow/" tooltip="设备借用"></node>
    <node id="baofeishengqing" guid="" name="报废申请" url="/Office/Lists/ScrapApply/" tooltip="报废申请"></node>
    <node id="shebeizengzhibiao" guid="" name="设备增值表" url="/Office/Lists/EquipmentApply/" tooltip="设备增值表"></node>
  </node>
  <node id="youxiantv" guid=""  name="有线电视管理系统" url="" tooltip="有线电视管理系统">
  </node>
  <node id="baogaotingxt" guid="" name="100号学术报告厅管理系统" url="" tooltip="100号学术报告厅管理系统">
   
  </node>
</Tree>

  评论这张
 
阅读(6)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018