Bind a Treeview tree

Source: Internet
Author: User

 

I:

Effect

<Form ID = "form1" runat = "server">
<Asp: Treeview id = "treeview1" runat = "server" Height = "300px" width = "200px" selectaction = "Expand">
</ASP: Treeview>
</Form>

 

Protected void page_load (Object sender, eventargs E)
{
If (! This. ispostback)
{
String where = "and com_istrue = 0 and com_status = 2 ";
Bdtree (where );
}
}

Private list <t_commoditytype> List = new list <t_commoditytype> ();
Private void bdtree (string where)
{
List = BLL. getmodellist (where );
Addtree ("", null );
}

Public void addtree (string parentid, treenode pnode)
{
If (list. Where (O => O. com_parentid.tostring (). Equals (parentid). Count () = 0)
Return;
List <t_commoditytype> clist = List. Where (O => O. com_parentid.tostring (). Equals (parentid). tolist ();
Foreach (t_commoditytype DC in clist)
{
Treenode node = new treenode ();
If (pnode = NULL)
{
Node. value = Dc. com_id.tostring ();
Node. Text = Dc. com_name;
Node. navigateurl = "javascript: saveseldata ('" + node. Text + "', '" + node. Value + "');";
Treeview1.nodes. Add (node );
Addtree (DC. com_id.tostring (), node );
}
Else
{
Node. value = Dc. com_id.tostring ();
Node. Text = Dc. com_name;
Node. navigateurl = "javascript: saveseldata ('" + node. Text + "', '" + node. Value + "');";
Pnode. childnodes. Add (node );
Addtree (DC. com_id.tostring (), node );
}
}
}

II:

 

Click "Fruit"

 

<Div>
<Asp: Treeview id = "treeview1" runat = "server" Height = "300px" width = "200px" selectaction = "Expand"
Showcheckboxes = "all" onselectednodechanged = "treeview?selectednodechanged" onLoad = "treeview#load">
</ASP: Treeview>
</Div>

 

 

Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Bdtree ();
}
}
Private list <t_commoditytype> List = new list <t_commoditytype> ();
Private void bdtree ()
{
List = BLL. getmodellist ("and com_istrue = 0 and com_status = 2 ;");
Addtree ("00000000-0000-0000-0000-000000000000", null );
}
Public void addtree (string parentid, treenode pnode)
{
If (list. Where (O => O. com_parentid.tostring (). Equals (parentid). Count () = 0)
Return;
List <t_commoditytype> clist =
List. Where (O => O. com_parentid.tostring (). tolower (). Equals (parentid. tolower (). tolist ();
Foreach (t_commoditytype DC in clist)
{
Treenode node = new treenode ();
If (pnode = NULL)
{
Node. value = Dc. com_id.tostring ();
Node. Text = Dc. com_name;
Node. showcheckbox = true;
Treeview1.nodes. Add (node );
Addtree (DC. com_id.tostring (), node );
}
Else
{
Node. value = Dc. com_id.tostring ();
Node. Text = Dc. com_name;
Node. showcheckbox = true;
// Node. selectaction = treenodeselectaction. selectexpand;
Pnode. childnodes. Add (node );
Addtree (DC. com_id.tostring (), node );
}

}
}
Protected void treeview1_selectednodechanged (Object sender, eventargs E)
{
If (treeview1.selectednode. Checked)
{
Treeview1.selectednode. Checked = false;
If (treeview1.selectednode. childnodes. Count> 0)
{
Foreach (treenode N in treeview1.selectednode. childnodes)
{
N. Checked = true;
}
}
Return;
}
Treeview1.selectednode. Checked = true;

If (treeview1.selectednode. childnodes. Count> 0)
{
Foreach (treenode N in treeview1.selectednode. childnodes)
{
N. Checked = true;
}
}
If (treeview1.selectednode. parent! = NULL)
{
// Treeview1.selectednode. Parent. Checked = true;
}
}

 

Protected void treeview1_load (Object sender, eventargs E)
{
If (! Object. Equals (null, treeview1.selectednode ))
{
Treeview1.selectednode. Select ();
Treeview1.selectednode. Selected = false;
}
}

Bind a Treeview tree

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.