Implement the binding of the Treeview Control Using recursive Methods
Protected void page_load (Object sender, eventargs E)
{
If (this. Page. ispostback)
Return;
Treenode node = new treenode ();
Node. Text = "China ";
Node. value = "-1 ";
Treeview1.datasource = NULL; // treeview1 is empty
Treeview1.nodes. Add (node );
Treeview1.collapseall ();
Bindtreeview (0, node );
}
# Region Binding data
// Self-called entry condition
Private void bindtreeview (INT parentid, treenode nodes)
{
Province Pclass = new province ();
Pclass. ID = parentid. tostring ();
String SQL = "select * from province where code = '" + Pclass. ID "'";
Dataset DS = sqlfunction. returndataset (SQL );
Treenode node;
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Node = new treenode ();
Node. Text = Ds. Tables [0]. Rows [I] ["name"]. tostring ();
Node. value = Ds. Tables [0]. Rows [I] ["ID"]. tostring ();
Nodes. childnodes. Add (node );
Bindtreeview (convert. toint32 (Ds. Tables [0]. Rows [I] ["ID"]), node );
}
}
# Endregion
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
/// Import the namespace
Using system. Web. UI. webcontrols;
/// <Summary>
/// Summary description for treeviewdataleveldisplay
/// </Summary>
Public class treeviewdataleveldisplay
{
# Region declare global variables
Softwaretailor. LMS. model. Department model = new softwaretailor. LMS. model. Department ();
# Endregion
Public treeviewdataleveldisplay ()
{
//
// Todo: Add constructor logic here
//
}
/// <Summary>
/// Implement data binding for tree controls
/// </Summary>
/// <Param name = "Treeview"> tree control </param>
/// <Param name = "parentid"> parent id </param>
Public static void treeviewdataband (Treeview, guid parentid)
{
Treeview. nodes. Clear ();
List <softwaretailor. LMS. model. Department> List = softwaretailor. LMS. BLL. Department. getlistbyparentid (parentid, "createddate ASC ");
Treenode node;
If (list! = NULL & list. Count> 0)
{
Foreach (softwaretailor. LMS. model. Department model in List)
{
Node = new treenode ();
Node. Text = model. departmentname;
Node. value = model. inclumentid. tostring ();
Gettreeviewdataleveldisplay (model. extends mentid, node );
Treeview. nodes. Add (node );
}
}
}
/// <Summary>
/// Display data at the tree control level
/// </Summary>
/// <Param name = "parentid"> parent id </param>
/// <Param name = "nodes"> tree node </param>
Public static void gettreeviewdataleveldisplay (guid parentid, treenode nodes)
{
List <softwaretailor. LMS. model. Department> List = new list <softwaretailor. LMS. model. Department> ();
List = softwaretailor. LMS. BLL. Department. getlistbyparentid (parentid, "createddate ASC ");
If (list = NULL | list. Count = 0)
{
Return;
}
Treenode node;
Foreach (softwaretailor. LMS. model. Department model in List)
{
Node = new treenode ();
Node. Text = model. departmentname;
Node. value = model. inclumentid. tostring ();
Nodes. childnodes. Add (node );
Gettreeviewdataleveldisplay (model. extends mentid, node );
}
}
}