Multi-level tree row Control Loading
1. Download the Microsoft iewebcontrols. MSI tree control. After installation, go to Visual Studio. NET
In the toolbox of "Add/Remove items", select ". NET Framework components" and add "Treeview control ".
2. reference the "Microsoft. Web. UI. webcontrols" Control in the project reference.
3. html-related languages using tree controls
4. Add using Microsoft. Web. UI. webcontrols to the preceding reference. Otherwise, a compilation error occurs: the type or namespace name "treenode" cannot be found (whether the using command orProgramSet Reference ?)
Register tagprefix = "iewc" namespace = "Microsoft. Web. UI. webcontrols" assembly = "Microsoft. Web. UI. webcontrols, version = 1.0.2.226" %>
<Iewc: Treeview id = "treeview1" runat = "server" imageurl = "close.gif" expandedimageurl = "open.gif" enableclientscript = "false"> </iewc: Treeview>
<Tr> <TD> <asp: Label id = "lbltypename" runat = "server" text = "name:"> </ASP: Label> </TD>
<TD> <asp: textbox id = "tbname" runat = "server"> </ASP: textbox> </TD>
</Tr>
<Tr> <TD> <asp: button id = "btnadd" runat = "server" text = "add"> </ASP: button>
<Asp: button id = "btnupdate" runat = "server" text = "modify"> </ASP: button>
<Asp: button id = "btndelete" runat = "server" width = "48px" text = "delete"> </ASP: button>
</TD>
</Tr>
<Asp: Label id = "LBID" runat = "server" visible = "false"> </ASP: Label> to record the ID of the parent item
4.. CSCode
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
If (! This. ispostback)
{
// Initialize the connection string
This. LBID. Text = "1 ";
Conn. open ();
Sqldataadapter ADP = new sqldataadapter ("select * From tbtree order by ID", Conn );
Dataset DS = new dataset ();
ADP. Fill (DS );
This. viewstate ["ds"] = Ds;
// Call a recursive function to generate a Tree Structure
Addtree (1, (treenode) null );
}
}
// Recursively add Tree nodes
Public void addtree (INT parentid, treenode pnode)
{
Dataset DS = (Dataset) This. viewstate ["ds"];
Dataview dvtree = new dataview (Ds. Tables [0]);
// Filter the parentid to obtain all the current subnodes.
Dvtree. rowfilter = "[parentid] =" + parentid;
Foreach (datarowview row in dvtree)
{
Treenode node = new treenode ();
If (pnode = NULL)
{// Add a root node
Node. Text = row ["context"]. tostring ();
Node. ID = row ["ID"]. tostring ();
Treeview1.nodes. Add (node );
Node. Expanded = false;
Addtree (int32.parse (row ["ID"]. tostring (), node); // recursive again
}
Else
{//? Add the subnode of the current node
Node. Text = row ["context"]. tostring ();
Node. ID = row ["ID"]. tostring ();
Pnode. nodes. Add (node );
Node. Expanded = false;
Addtree (int32.parse (row ["ID"]. tostring (), node); // recursive again
}
}
}
// Select the node Value
Private void treeviewincluselectedindexchange (Object sender, Microsoft. Web. UI. webcontrols. treeviewselecteventargs E)
{
This. tbname. Text = This. treeview1.getnodefromindex (this. treeview1.selectednodeindex). text;
This. LBID. Text = This. treeview1.getnodefromindex (this. treeview1.selectednodeindex). ID;
}
// Update
Private void btnupdate_click (Object sender, system. eventargs E)
{
String SQL = "Update tbtree set context = '" + this. tbname. text + "'where id = '" + this. LBID. text. replace ("'","''"). trim () + "'";
Sqlcommand mycommand = new sqlcommand (SQL, Conn );
Conn. open ();
Mycommand. executenonquery ();
Conn. Close ();
// Call a recursive function to generate a Tree Structure
This. response. Redirect ("treerole. aspx ");
This. tbname. Text = "";
}
// Delete related node Information
Private void btndelete_click (Object sender, system. eventargs E)
{
String stringdelete = "delete from tbtree where id = '" + this. LBID. Text. Replace ("'", "'' "). Trim () + "'";
Sqlcommand inst = new sqlcommand (stringdelete, Conn );
Conn. open ();
Inst. executenonquery ();
Conn. Close ();
This. response. Write ("<script language = JavaScript> alert ('deleted successfully! '); </SCRIPT> ");
// Call a recursive function to generate a Tree Structure
This. response. Redirect ("treerole. aspx ");
This. tbname. Text = "";
}
// Add node Information
Private void btnadd_click (Object sender, system. eventargs E)
{
String SQL = "insert into tbtree (context, parentid) values ('" + this. tbname. text. replace ("'","''"). trim () + "','" + this. LBID. text. replace ("'","''"). trim () + "')";
Sqlcommand mycmd = new sqlcommand (SQL, Conn );
Conn. open ();
Mycmd. executenonquery ();
Conn. Close ();
// Call a recursive function to generate a Tree Structure
This. response. Redirect ("treerole. aspx ");
This. tbname. Text = "";
}
5. Data Table tbtree ID (automatically identified by INT), context (nvarchar), and parentid (INT)
The parentid of the subitem is the ID of the parent item, which makes it clearer when multiple levels are loaded cyclically.