This function has time to do. Take notes first!
ArticleReprinted: http://hi.baidu.com/51zt/blog/item/d8bffe19c28a527adbb4bde5.html
ASP. NET 2.0 reads data from the database to generate a hierarchical structured Treeview
These days, I helped my friends study an e-commerce website. When I encountered an item classification problem, I wanted to use the Treeview control to create an infinite classification tree. Now I have finally solved it.
The solution is as follows: view the properties of the Treeview control and you will find that one of them is"Populatnodesfromclient"
Indicates whether to fill the node from the client.
After reading the explanation, of course, the value is "true ".
After "populatnodesfromclient" is assigned a value of "true", one attribute event in the Treeview is a treenodepopulate event, which is triggered when the treenode is being filled, when writing this event method, you can write and read the corresponding sub-node content from the database.
Therefore, when a node is added to the treenode, the event is triggered to fill its subnodes.
You can generate an empty statement as follows:
Protected void treeviewinclutreenodepopulate (Object sender, treenodeeventargs E)
{
//
}
The following is a complete example (the class connected to the database is not written, and only the class in default. aspx is written.Code):
Effect reference:
The Code is as follows:
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. oledb; public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
Initroot (trvmain );
}
}
/// <Summary>
/// Use SQL statement parameters to generate the DS
/// </Summary>
/// <Param name = "sqlstr"> SQL statement </param>
/// <Returns> </returns>
Private dataset createdataset (string sqlstr)
{
Try
{
Clsconn myconn = new clsconn ();
Myconn. open ();
Oledbdataadapter Cd = new oledbdataadapter ();
CD. selectcommand = new oledbcommand (sqlstr, myconn. changetype ());
CD. selectcommand. commandtype = commandtype. text;
Dataset dstree = new dataset ();
CD. Fill (dstree, "table ");
Myconn. Close ();
Return dstree;
}
Catch
{
Console. writeline ("exception in main :");
Return NULL;
}
}
/// <Summary>
/// Insert the root node
/// </Summary>
/// <Param name = "NDS"> Treeview Control ID </param>
Private void initroot (Treeview NDS)
{
Try
{
Dataview dvtree = new dataview ();
Treenode tmpnd;
// Call the DS
Dataset DS = createdataset ("select * from type where pid = 0 ");
// Fill DS data in the dvtree
Dvtree. Table = Ds. Tables ["table"];
Foreach (datarowview drvtree in dvtree)
{
// Create the node to be added
Tmpnd = new treenode ();
Tmpnd. value = drvtree ["TID"]. tostring ();
Tmpnd. Text = drvtree ["tname"]. tostring ();
// Add a root node to the control
NDS. nodes. Add (tmpnd );
// Set it to automatically check whether there are subnodes
Tmpnd. populateondemand = true;
Tmpnd. selectaction = treenodeselectaction. Expand;
}
}
Catch
{
//
}
}
/// <Summary>
/// Add a subnode
/// </Summary>
/// <Param name = "TND"> currently added node </param>
Private void addchild (treenode TND)
{
Try
{
Dataview dvtree = new dataview ();
Treenode tmpnd;
// Call the DS
Dataset DS = createdataset ("select * from type ");
// Fill DS data in the dvtree
Dvtree. Table = Ds. Tables ["table"];
// Filter the parent node ID of the current node
Dvtree. rowfilter = "pid =" + TND. value. tostring () + "";
Foreach (datarowview drvtree in dvtree)
{
// Create the node to be added
Tmpnd = new treenode ();
Tmpnd. value = drvtree ["TID"]. tostring ();
Tmpnd. Text = drvtree ["tname"]. tostring ();
// Add it to the current node
TND. childnodes. Add (tmpnd );
// Set whether a subnode exists in the automatic search.
Tmpnd. populateondemand = true;
}
If (TND. childnodes. Count! = 0)
TND. selectaction = treenodeselectaction. Expand;
}
Catch
{
//
}
}
/// <Summary>
/// Automatically fill nodes
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void trvmain_treenodepopulate (Object sender, treenodeeventargs E)
{
// Add a subnode to the current node
Addchild (E. node );
}
Protected void btnaddchild_click (Object sender, eventargs E)
{
Try
{
Clsconn myconn = new clsconn ();
Myconn. open ();
Oledbdataadapter Cd = new oledbdataadapter ();
String sqlstr = "insert into type (tname, pid) values ('" + txtchildname. text. trim () + "'," + ddlmain. selectedvalue. tostring () + ")";
CD. selectcommand = new oledbcommand (sqlstr, myconn. changetype ());
CD. selectcommand. executenonquery ();
Reset ();
Ddlmain. databind ();
}
Catch
{
//
}
}
/// <Summary>
/// Refresh the Treeview
/// </Summary>
Private void reset ()
{
Trvmain. nodes. Clear ();
Initroot (trvmain );
Trvmain. expandall ();
}
Protected void btnreset_click (Object sender, eventargs E)
{
Reset ();
}
}