Database structure:
Classidid PRIMARY Key
Jobclassname the corresponding type name
ClassName the ID of the corresponding parent class
Usual procedure:
Copy Code code 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 approach is that each parent category has to establish a connection, completely wasting resources
Now remove all the categories at once and use the DataView RowFilter property to do multiple filters
Critical code
Copy Code code 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 way, there is no need to waste resources.
In fact, this article is a little far-fetched, general classification are rarely made changes, directly with the cache or static processing can be, just thought of the record O (∩_∩) o~.