Binding TreeView and selecting selected values

Source: Internet
Author: User

 

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;
}
}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.