1. Bind The treeview
Private void DrawTree ()
{
Var tblist = tbdu. tbDepartments. Where (a => a. pid = "0 ");
If (tblist. ToList (). Count! = 0)
{
Foreach (tbDepartment item in tblist)
{
TreeNode tbadd = new TreeNode ();
Tbadd. Text = item. name;
Tbadd. Value = item. id;
TreeView1.Nodes. Add (tbadd );
DrawChild (tbdu, tbadd );
Tbadd. ExpandAll ();
}
}
}
Private void DrawChild (tbDepartmentAndUserDataContext dc, TreeNode tbParent)
{
Var tblist = dc. tbparts. Where (a => a. pid = tbParent. Value );
Foreach (tbDepartment items in tblist)
{
TreeNode tbAdd = new TreeNode ();
TbAdd. Text = items. name;
TbAdd. Value = items. id;
TbParent. ChildNodes. Add (tbAdd );
DrawChilds (dc, tbAdd );
TbAdd. ExpandAll ();
}
}
Private void DrawChilds (tbDepartmentAndUserDataContext dc, TreeNode tbParent)
{
Var childs = from a in dc. tbdocumments
Join B in dc. tbDepartmentUsers
On a. id equals B. dpt_id
Select B;
Foreach (tbDepartmentUser useritem in childs)
{
TreeNode chadd = new TreeNode ();
Chadd. Text = useritem. userid;
Chadd. Value = useritem. dpt_id;
TbParent. ChildNodes. Add (chadd );
Chadd. ExpandAll ();
}
}
2. Obtain the selected value in the treeview.
List <string> list = new List <string> ();
Protected void butok_Click (object sender, EventArgs e)
{
If (TreeView1.Nodes. Count! = 0)
{
Foreach (TreeNode node in TreeView1.CheckedNodes)
{
List. Add (node. Text );
}
If (list. Count = 0)
{
Return;
}
If (list. Count> = 2)
{
This. lblonlytow. Visible = true;
This.txt fenpei. Text = "";
Return;
}
Else
{
This.txt fenpei. Text = list [0];
This. lblonlytow. Visible = false;
}
}
}