兩種方式展示資料庫中無限級樹行結構

來源:互聯網
上載者:User

下面介紹個人在開發中用到的兩種無限級菜單的展示方式,第一種是將菜單放置到DropDownList控制項中,這樣直觀明了,代碼如下:

  1.     private void MakeTree()
  2.     {
  3.         int mainid = 0;
  4.         DataTable dt = GetTable(mainid);
  5.         try
  6.         {
  7.             if (dt != null)
  8.             {
  9.                 for (int i = 0; i < dt.Rows.Count; i++)
  10.                 {
  11.                     string t = "|- " + dt.Rows[i]["DEPT_NAME"].ToString().Trim();
  12.                     string v = dt.Rows[i]["ID"].ToString();
  13.                     ListItem li = new ListItem(t, v);
  14.                     this.DropDownList1.Items.Add(li);
  15.                     AddSub(int.Parse(dt.Rows[i]["ID"].ToString()), 0);
  16.                 }
  17.             }
  18.         }
  19.         catch (Exception ex)
  20.         {
  21.             Response.Write(ex.Message);
  22.         }
  23.     }
  24.     private void AddSub(int pID, int level)
  25.     {
  26.         DataTable dt = GetTable(pID);
  27.         try
  28.         {
  29.             if (dt != null)
  30.             {
  31.                 for (int i = 0; i < dt.Rows.Count; i++)
  32.                 {
  33.                     string blank = "| ";
  34.                     for (int m = 1; m <= level; m++)
  35.                     {
  36.                         blank = blank + " ";
  37.                     }
  38.                     string t = blank + "|- " + dt.Rows[i]["Dept_Name"].ToString();
  39.                     string v = dt.Rows[i]["ID"].ToString();
  40.                     ListItem li = new ListItem(t, v);
  41.                     this.DropDownList1.Items.Add(li);
  42.                     if (dt.Rows.Count > 0)
  43.                     {
  44.                         AddSub(int.Parse(dt.Rows[i]["ID"].ToString()), level + 1);
  45.                     }
  46.                     else
  47.                     {
  48.                         AddSub(int.Parse(dt.Rows[i]["Parentid"].ToString()), level + 1);
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.         catch (Exception ex)
  54.         {
  55.             Response.Write(ex.Message);
  56.         }
  57.     }

第二種方式是幫定到TreeView控制項中,代碼如下:

  1. private void AddTreeNodes(int pID, TreeNode node)
  2.     {
  3.         DataTable dt = GetTable(pID);
  4.         try
  5.         {
  6.             if (dt != null)
  7.             {
  8.                 for (int i = 0; i < dt.Rows.Count; i++)
  9.                 {
  10.                     TreeNode nodes = new TreeNode();
  11.                     nodes.Text = dt.Rows[i]["Dept_Name"].ToString();
  12.                     nodes.Value = dt.Rows[i]["ID"].ToString();
  13.                     node.ChildNodes.Add(nodes);
  14.                     if (dt.Rows.Count > 0)
  15.                     {
  16.                         AddTreeNodes(int.Parse(dt.Rows[i]["ID"].ToString()), nodes);
  17.                     }
  18.                     else
  19.                     {
  20.                         AddTreeNodes(int.Parse(dt.Rows[i]["Parentid"].ToString()), nodes);
  21.                     }
  22.                 }
  23.             }
  24.         }
  25.         catch (Exception ex)
  26.         {
  27.             Response.Write(ex.Message);
  28.         }
  29.     }
  30.     protected void MakeTree()
  31.     {
  32.         int mainid = 0;
  33.         DataTable dt = GetTable(mainid);
  34.         try
  35.         {
  36.             if (dt != null)
  37.             {
  38.                 for (int i = 0; i < dt.Rows.Count; i++)
  39.                 {
  40.                     TreeNode nodes = new TreeNode();
  41.                     nodes.Text = dt.Rows[i]["DEPT_NAME"].ToString();
  42.                     nodes.Value = dt.Rows[i]["ID"].ToString();
  43.                     this.TreeView1.Nodes.Add(nodes);
  44.                     AddTreeNodes(int.Parse(dt.Rows[i]["ID"].ToString()), nodes);
  45.                 }
  46.             }
  47.         }
  48.         catch (Exception ex)
  49.         {
  50.             Response.Write(ex.Message);
  51.         }
  52.     }
  53.     private DataTable GetTable(int refid)
  54.     {
  55.         DataTable dt = new DataTable();
  56.         BussinessDataAccess DBA = new BussinessDataAccess();
  57.         try
  58.         {
  59.             dt = DBA.SelectDataBySql("SELECT * FROM USER_ADDRESS_DEPTINFO WHERE ParentId = '" + refid + "'", "tablename");
  60.         }
  61.         catch (Exception ex)
  62.         {
  63.             Response.Write(ex.Message);
  64.         }
  65.         return dt;
  66.     }

還可以綁定到其他的的控制項,如Menu,SiteMap控制項等.資料庫結構如下所示.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.