Asp.net|treeview
First you need to create a database table test with fields including Nodeid, ParentID, NodeName, Adderss, ICON, Linkurl
Nodeid is the unique identification of a node in the database, where it is set as an identifier for convenience;
ParentID represents the ancestor node ID of each node, or 0 if the node has no superior;
NodeName is the name that the node displays on the page;
The above three fields should not be empty
Adderss address; icon node picture; Linkurl node link address;
The above three fields with and not necessarily, due to the actual situation, here because the use of so listed to
Next, add a TreeView control to the design form
If you have not yet added this control, download and install it at the following address, add a reference Microsoft.Web.UI.WebControls.dll, and then add controls to the Toolbox
Http://msdn.microsoft.com/archive/en-us/samples/internet/asp_dot_net_servercontrols/webcontrols/default.asp
Here's the code for the Aspx.cs section.
Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using Microsoft.Web.UI.WebControls;
Namespace IOA
{
<summary>
Summary description of the WebForm1.
</summary>
public class WebForm1:System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
Protected Classes.deptartment mydt=new IOA. Classes.deptartment ()//I write requests for links to databases and database operations in this class
The link to the database here is not specifically written out, linked to the database after
Pass the SELECT * from Test return value to the DS change in the program
, remember that the type of value returned is a dataset
private void Page_Load (object sender, System.EventArgs e)
{
Inittreeview (this. Treeview1.nodes);
Place user code here to initialize page
}
public void Inittreeview (treenodecollection node)
{
This. Inittree (node, "0");
}
public void Inittree (TreeNodeCollection nds,string parentid)
{
DataSet ds=new DataSet ();
Ds=mydt.treeinfo ();
DataView dv = new DataView ();
TreeNode TMPND;
String intId;
Dv. Table = ds. Tables[0];
Dv. RowFilter = "ParentID =" + ParentID;
foreach (DataRowView DRV in DV)
{
TMPND = new TreeNode ();
Tmpnd.id = drv["NODEID"]. ToString ();
if (drv["Linkurl"]. ToString (). Trim ()!= "")
{
Tmpnd.text = "<a href = '" +drv["Linkurl"]. ToString (). Trim () + "' target= ' MainFrame ' >" +drv["nodename". ToString () + "</a>";
}
Else
{
Tmpnd.text = drv["NodeName"]. ToString ();
}
Nds.add (TMPND);
IntId = drv["ParentID"]. ToString ();
Inittree (tmpnd.nodes,tmpnd.id);
}
}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
}
}