今天做项目用到了TreeView。
这是绑定:
View Code
1 public void Bind()
2 {
3 DataTable dt = UserDAL.GetRole("and parentid=0 ");
4 tvShow.ShowCheckBoxes = TreeNodeTypes.All;
5 for (int i = 0; i < dt.Rows.Count; i++)
6 {
7 TreeNode nd = new TreeNode();
8 nd.ShowCheckBox = true;
9 nd.Text = dt.Rows[i]["FuncName"].ToString();
10 nd.Value = dt.Rows[i]["FuncId"].ToString();
11 DataTable dt2 = UserDAL.GetRole(" and parentid=" + Convert.ToInt32(nd.Value) + "");
12
13 for (int j = 0; j < dt2.Rows.Count; j++)
14 {
15 TreeNode tn = new TreeNode();
16 tn.ShowCheckBox = true;
17 tn.Text = dt2.Rows[j]["FuncName"].ToString();
18 tn.Value = dt2.Rows[j]["FuncId"].ToString();
19 nd.ChildNodes.Add(tn);
20 }
21 tvShow.Nodes.Add(nd);
22 }
23 }
这是获取(递归):
View Code
1 public void bbs(TreeNodeCollection coll)
2 {
3 DataTable dt = UserDAL.GetGrpFuncList(int.Parse(ViewState["userid"].ToString()));
4 foreach (TreeNode aas in coll)
5 {
6 for (int i = 0; i < dt.Rows.Count; i++)
7 {
8 if (Convert.ToInt32(aas.Value) == Convert.ToInt32(dt.Rows[i]["funcid"].ToString()))
9 {
10 aas.Checked = true;
11 }
12 }
13 bbs(aas.ChildNodes);
14 }
15 }