Another use of asp.net dataview for infinitus Classification

Source: Internet
Author: User

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 ~.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.