About Asp.net automatically generated tree drop-down list (Database Version)

Source: Internet
Author: User

After searching for the tree for a long time on the Internet, I did not find the tree drop-down list, so I wrote one myself. (I am busy with work and have no time. It's easy to share this time, what are the shortcomings?ProgramMembers and comrades give a comment

Function: Infinite tree drop-down list, bound to a database

Table Structure: at least two fields, [ID, father_id] (preferably varchar type), are required to form a parent-child relationship.

DetailsCode:

// Data Acquisition
Protected void dropdatacon ()
{
Dataset DS = getdroplistdate (); // This is the data table that obtains the drop-down list, that is, the table above.
This. viewstate ["dataset"] = Ds;
Adddropdownlistitem (start ID );
Drop_dirlist.items.insert (0, new listitem (@ "/", start ID ));
}

// Recursively generate a tree drop-down list

Private Static string nbsp = ""; // public variable (required)
Protected void adddropdownlistitem (string ID) // This ID parameter is generated from that level (start ID)
{
Dataset DS = (Dataset) This. viewstate ["dataset"];
Dataview DV = new dataview (Ds. Tables [0]);
DV. rowfilter = "father_id = '" + ID + "'";

Foreach (datarowview DRV in DV)
{

Drop_dirlist.items.add (New listitem (nbsp + DRV ["dir_name"]. tostring (), DRV ["dir_id"]. tostring ()));
Nbsp + = "";
Adddropdownlistitem (DRV ["dir_id"]. tostring ());
Nbsp = nbsp. Remove (nbsp. Length-2 );
}
}

 

 

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.