1.没有用递归写的代码 private System.Text.StringBuilder OnLoad() { System.Text.StringBuilder sb = new System.Text.StringBuilder(); //顶级父节点 sb.Append("[{text:'武汉市金讯科技有限公司',cls:'folder',leaf:false,checked:false,icon :'../../images/LOGO.png',children:"); //取得所有菜单和功能点数据集合 System.Data.DataSet dsMenu = this.GetMenuData(); string strAction = ""; //得到XML根节点条件 string strCondition = string.Format("level=1 or menucode in ('{0}') ", strAction); AppData.DataSetHelper dshelp = new AppData.DataSetHelper(ref dsMenu); //父级菜单格式化 string father = "text:'{0}',cls:'folder',checked:false,leaf:false,id:'{1}',children: ["; //第一层父级目录 System.Data.DataTable dtModel = dshelp.SelectJoinInto("menumodel", dsMenu.Tables[0], "menucode,menuname", strCondition, "menucode"); //开始 json权限菜单拼接 string strMenu = ""; if (dtModel.Rows.Count > 0) { strMenu = "["; for (int iRow = 0; iRow < dtModel.Rows.Count; iRow++) { strMenu += "{"; //第一层子菜单 System.Data.DataTable dtMenu = dshelp.SelectJoinInto(dtModel.Rows[iRow]["menuCode"] + "", dsMenu.Tables[0], "menucode,menuname,parentMenuCode", string.Format("parentMenuCode='{0}' or menucode in ('{1}') ", dtModel.Rows[iRow]["menuCode"] + "", strAction), "menucode"); //先加载第一层菜单的名称 strMenu += string.Format(father, dtModel.Rows[iRow]["menuName"] + "", dtModel.Rows[iRow]["menuCode"] + ""); for (int i = 0; i < dtMenu.Rows.Count; i++) { //子菜单下面的操作项 DataTable DTChild = dshelp.SelectJoinInto(dtMenu.Rows[i]["menuCode"] + "", dsMenu.Tables[0], "menucode,menuname", string.Format("parentMenuCode='{0}'", dtMenu.Rows[i]["menuCode"] + ""), "menucode"); //当子菜单下面有操作的时候 if (DTChild.Rows.Count > 0) { //此时为二层父节点菜单非栏目菜单 strMenu += "{"; strMenu += string.Format("text:'{0}',cls:'folder',leaf: false,checked: false,id:{1},children: [", dtMenu.Rows[i]["menuName"], dtMenu.Rows[i]["menuCode"]); for (int k = 0; k < DTChild.Rows.Count; k++) { //加载第三层的菜单信息 strMenu += "{"; strMenu += string.Format("text:'{0}',leaf: true,checked: false,id:{1}", DTChild.Rows[k]["menuName"], DTChild.Rows[k]["menuCode"]); strMenu += "},"; } strMenu = strMenu.Substring(0, strMenu.Length - 1); strMenu += "]"; strMenu += "},"; } else { //加载第一层菜单的子菜单 strMenu += "{"; strMenu += string.Format("text:'{0}',leaf: true,checked: false,id:{1}", dtMenu.Rows[i]["menuName"], dtMenu.Rows[i]["menuCode"]); strMenu += "},"; } } strMenu = strMenu.Substring(0, strMenu.Length - 1); strMenu += "]"; //一个父级菜单加载完毕 strMenu += "},"; } } strMenu = strMenu.Substring(0, strMenu.Length - 1); strMenu += "]"; sb.Append(strMenu); //获取XML中所有节点毕 sb.Append("}]"); return sb; } 2用递归写了的代码 public void GetTree(DataTable DT, int PID, int Depth) { //最后输出的字符串信息 // string Results = string.Empty; DataView DV = DT.DefaultView; DV.RowFilter = "parentMenuCode=" + PID; //2层以内使用 string Desc = string.Empty; if (Depth == 1) { //初始化根节点的字符串格式 Desc = "text:'{0}',cls:'folder',checked:false,leaf:false,id:'{1}',children: ["; } else { //初始化单个节点字符串格式 Desc = "text:'{0}',leaf: true,checked: false,id:{1}"; } foreach (DataRowView Drv in DV) { Res += "{"; //根节点下面的子目录数量 int cnt = 0; int d = 0; if (ViewState["cnt"] != null) { cnt = int.Parse(ViewState["cnt"].ToString()); } if (ViewState["d"] != null) { d = int.Parse(ViewState["d"].ToString()); } if (Depth == 1) { //得到根目录下面有多少子目录 string where1 = "parentMenuCode=" + int.Parse(Drv["menuCode"].ToString()); cnt = dshelp.SelectJoinInto("menumodel", dsMenu.Tables[0], "menucode,menuname,parentMenuCode,level", where1, "menucode").Rows.Count; if (ViewState["cnt"] != null) { ViewState["cnt"]=0; } ViewState["cnt"] = cnt; //清空下级子目录 if (ViewState["c"] != null) { ViewState["c"] = 0; } Res += string.Format(Desc, Drv["menuName"].ToString(), Drv["menuCode"]); } else if (Depth == 2) { //循环一次计算一次当前子目录直到最后一层然后用]}关闭 int c = 0; if (ViewState["c"] != null) { c = int.Parse(ViewState["c"].ToString()); } c++; ViewState["c"] = c; string where = "parentMenuCode=" + int.Parse(Drv["menuCode"].ToString()); DataTable NewDT=dshelp.SelectJoinInto("menumodel", dsMenu.Tables[0], "menucode,menuname,parentMenuCode,level", where, "menucode"); if (NewDT.Rows.Count > 0) { if (ViewState["d"] != null) { ViewState["d"] = 0; } //下级菜单的数量 d = NewDT.Rows.Count; ViewState["d"] = d; Res += string.Format("text:'{0}',cls:'folder',checked:false,leaf:false,id:'{1}',children: [", Drv["menuName"].ToString(), Drv["menuCode"]); } else { //这个就是最后一层所以要加上]} if (c == cnt) { Res += string.Format(Desc, Drv["menuName"].ToString(), Drv["menuCode"]); Res += "}]},"; } else { Res += string.Format(Desc, Drv["menuName"].ToString(), Drv["menuCode"]); Res += "},"; } } } else { int e = 0; if (ViewState["e"] != null) { e = int.Parse(ViewState["e"].ToString()); } e++; ViewState["e"] = e; if (e == d) { Res += string.Format(Desc, Drv["menuName"].ToString(), Drv["menuCode"]); Res += "}]},"; } else { Res += string.Format(Desc, Drv["menuName"].ToString(), Drv["menuCode"]); Res += "},"; } } int n = int.Parse(Drv["level"].ToString()); n++; GetTree(DT, int.Parse(Drv["menuCode"].ToString()), n); } }