Database Structure:
Classidid primary key
Type name corresponding to jobClassName
Id of the parent class corresponding to ClassName
Common Practice:
Copy codeThe Code is as follows: private void Display (string parentid, String space)
{
DataTable dt;
String strSQL;
StrSQL = "Select * From Tree Where ParentID =" + parentid + "Order By ClassID DESC ";
SqlDataAdapter sda = new SqlDataAdapter (strSQL, conn );
DataSet ds = new DataSet ();
Sda. Fill (ds, "Tree ");
Dt = ds. Tables ["Tree"];
If (dt. Rows. Count> 0)
{
Foreach (DataRow dr in dt. Rows)
{
StrOpinion + = space + "<font color = red> [" + dr ["JobClassName"]. ToString () + "<br> ";
Display (dr ["ClassID"]. ToString (), "" + space, false );
}
}
}
Obviously, this method requires a connection for each parent category, which is a waste of resources.
Now all categories are retrieved at a time, and the RowFilter attribute of DataView is used to filter multiple times.
Key code
Copy codeThe Code is as follows: public partial class tree_Default: System. Web. UI. Page
{
DataTable dt = null;
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
Bind (0 );
}
}
Public void bind (int pid)
{
DataTable dt1 = bindTree (pid );
Foreach (DataRow dr in dt1.Rows)
{
Int id = Convert. ToInt32 (dr ["classid"]. ToString ());
If (pid = 0)
Response. write ("<div style = 'width: 100%; float: right; '> Else
Response. write ("<div style = 'width: 25%; float: left; '>" + dr ["jobclassname"]. toString () + "</div> ");
Bind (id );
}
}
Public DataTable bindTree (int pid)
{
If (dt = null)
Dt = new data (). getCatelogs ();
DataView root = dt. DefaultView;
Root. RowFilter = "Parentid =" + pid;
Return root. ToTable ();
}
}
In this case, there is no need to waste resources.
In fact, this article is somewhat far-fetched. Generally, the classification is rarely changed. You can simply use cache or static processing, but you just thought of recording O (∩ _ ∩) O ~.