using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text;using System.Data;using System.Web.UI;using System.Web.UI.WebControls;public partial class RoleDemo20150305 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { DataTable dt = createDT(); DataTable dtFinal = dt.Clone(); List<string> list = new List<string>(); string userStr = GetUserData(); if (userStr.Contains(",")) { string[] strs = userStr.Split(','); for (int i = 0; i < strs.Length; i++) { list.Add(strs[i]); } } else { list.Add(userStr); } for (int i = 0; i < list.Count; i++) { string module_id = list[i]; DataRow[] drSource = dt.Select(" module_id = '" + module_id + "'"); //最終DT是否存在該資料 DataRow[] drExist = dtFinal.Select(" module_id = '" + module_id + "'"); //最終DT是否存在該資料 if (drExist.Length == 0) { dtFinal.Rows.Add(drSource[0]["module_id"], GetPreTag(drSource[0]["module_level"]) + drSource[0]["module_name"], drSource[0]["module_fatherid"], drSource[0]["module_url"], drSource[0]["module_order"], drSource[0]["module_level"]); //添加記錄 } FindFatherFromDT(dt, drSource, dtFinal); } //顯示方式一 DataView dv = dtFinal.DefaultView; dv.Sort = "module_id asc"; DataTable dtNeed = dv.ToTable(); GridView1.DataSource = dtNeed; GridView1.DataBind(); //顯示方式二 StringBuilder builder = new StringBuilder(); for (int i = 0; i < dtNeed.Rows.Count; i++) { DataRow dr = dtNeed.Rows[i]; builder.AppendLine("<span data-id='" + dr["module_id"] + "' data-father='" + dr["module_fatherid"] + "' style='display:none'>" + dr["module_name"] + "</span>"); } lt1.Text = builder.ToString(); } /// <summary> /// 繼續追溯添加父類記錄 /// </summary> /// <param name="dt"></param> /// <param name="dtFinal"></param> protected void FindFatherFromDT(DataTable dt, DataRow[] drFather, DataTable dtFinal) { DataRow[] drFatherUp = dt.Select(" module_id = '" + drFather[0]["module_fatherid"] + "'"); DataRow[] drFatherUpExist = dtFinal.Select(" module_id = '" + drFather[0]["module_fatherid"] + "'"); if (drFatherUp.Length > 0 && drFatherUpExist.Length == 0) //該記錄存在父類且父類未添加到最終DT { dtFinal.Rows.Add(drFatherUp[0]["module_id"], GetPreTag(drFatherUp[0]["module_level"]) + drFatherUp[0]["module_name"], drFatherUp[0]["module_fatherid"], drFatherUp[0]["module_url"], drFatherUp[0]["module_order"], drFatherUp[0]["module_level"]); //繼續追溯添加父類記錄 FindFatherFromDT(dt, drFatherUp, dtFinal); } } #region 添加首碼 protected string GetPreTag(object num) { string pre = string.Empty; int i = Convert.ToInt32(num); if (i < 2) { } else if (i == 2) { pre += "└"; } else { pre += "└"; for (int j = 0; j < i - 2; j++) { pre += "─"; } } return pre; } #endregion #region 使用者測試資料 protected static string GetUserData() { DataTable dt = new DataTable(); dt.Columns.Add("userId"); dt.Columns.Add("userModuleOption"); dt.Rows.Add("張三", "M01010305,M010205,M0101030301"); return dt.Rows[0]["userModuleOption"].ToString(); } #endregion #region 建立資料 protected static DataTable createDT() { DataTable dt = new DataTable(); dt.Columns.Add("module_id"); dt.Columns.Add("module_name"); dt.Columns.Add("module_fatherid"); dt.Columns.Add("module_url"); dt.Columns.Add("module_order"); dt.Columns.Add("module_level"); dt.Rows.Add("C1", "全國", "0", "", "1", "1"); dt.Rows.Add("M01", "廣東", "C1", "", "1", "2"); //深圳 dt.Rows.Add("M0101", "深圳", "M01", "3.aspx", "100", "3"); dt.Rows.Add("M010101", "南山區", "M0101", "4.aspx", "1000", "4"); dt.Rows.Add("M010102", "羅湖區", "M0101", "", "1001", "4"); dt.Rows.Add("M010103", "福田區", "M0101", "", "1002", "4"); dt.Rows.Add("M010104", "寶安區", "M0101", "", "1003", "4"); dt.Rows.Add("M010105", "龍崗區", "M0101", "", "1004", "4"); //深圳 - 福田區 dt.Rows.Add("M01010301", "上梅林", "M010103", "", "1002001", "5"); dt.Rows.Add("M01010302", "下梅林", "M010103", "", "1002002", "5"); dt.Rows.Add("M01010303", "車公廟", "M010103", "", "1002003", "5"); dt.Rows.Add("M01010304", "竹子林", "M010103", "", "1002004", "5"); dt.Rows.Add("M01010305", "八卦嶺", "M010103", "", "1002005", "5"); dt.Rows.Add("M01010306", "華強北", "M010103", "", "1002006", "5"); //深圳 - 福田區 - 車公廟 dt.Rows.Add("M0101030301", "天安數位城", "M01010303", "", "100200301", "6"); //廣州 dt.Rows.Add("M0102", "廣州", "M01", "", "101", "3"); dt.Rows.Add("M010201", "越秀區", "M0102", "", "1105", "4"); dt.Rows.Add("M010202", "海珠區", "M0102", "", "1106", "4"); dt.Rows.Add("M010203", "天河區", "M0102", "", "1107", "4"); dt.Rows.Add("M010204", "白雲區", "M0102", "", "1108", "4"); dt.Rows.Add("M010205", "黃埔區", "M0102", "", "1109", "4"); dt.Rows.Add("M010206", "荔灣區", "M0102", "", "1110", "4"); dt.Rows.Add("M010207", "羅崗區", "M0102", "", "1111", "4"); dt.Rows.Add("M010208", "南沙區", "M0102", "", "1112", "4"); return dt; } #endregion}
更多關於C#相關內容感興趣的讀者可查看本站專題:《C#常見控制項用法教程》、《WinForm控制項用法總結》、《C#資料結構與演算法教程》、《C#物件導向程式設計入門教程》及《C#程式設計之線程提示總結》