asp.net infinite Class Class instance code _ Practical Skill

Source: Internet
Author: User
Tags parent directory
Looking for a long time on the internet did not find an example, no way, want to be lazy also stolen, or their own honest to write a bar. No more nonsense, below please see the detailed method I do.

1. Infinite classification must be database support, users can dynamically add their classification, the following is the structure of the database:


The database has three main fields, which is the most basic. The first is the primary key, the second is the name of the category, and the third is the parent directory ID number.

2. The result of the finished product: (is the most concerned about this ah?) See what it looks like and see how the program is.


See!! It looks like this, and this level of relationship is the result of the data shown above. What do you think? It's OK ...

3. The last is to provide the program, the program is actually a recursive algorithm.
Copy Code code as follows:

private void Getarticlecategory (string pid)
{
SqlConnection conn = new SqlConnection ("server=.; database=test;uid=sa;pwd=; ");
String sql = "Select Articlesgroup_id,groupname from Articlesgroup where articlesgroup_parent_id= @pid order by GroupName" ;
SqlCommand cmd = new SqlCommand (SQL, conn);
SqlParameter Pid = new SqlParameter ("@pid", SqlDbType.Int);
Pid.value = pid;
Cmd. Parameters.Add (Pid);
Conn. Open ();
SqlDataReader SDR = cmd. ExecuteReader ();
while (SDR. Read ())
{
This. DROPDOWNLIST1.ITEMS.ADD (New ListItem (Toadd + "" + sdr[1). ToString (), sdr[0]. ToString ()));
Toadd + = "─┴";
This. Getarticlecategory (Sdr[0]. ToString ());
Toadd = Toadd. Substring (0, Toadd.  LENGTH-2); At first there is no processing, the hierarchical relationship does not show up:
}
Sdr. Close ();
Conn. Close ();
}

Call method: called in Page_Load
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
THIS.KEYWORD.ATTRIBUTES.ADD ("onfocus", "if (this.value== ' key key ') {this.value= '};");
if (! Page.IsPostBack)
{
This. Getarticlecategory ("0");
}
This. DropDownList1.Items.Insert (0, New ListItem ("Search All", "all"));
}

In the class, you also define a variable
Copy Code code as follows:

private string toadd =  "├"; 
Related Article

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.