ASP. net c # generate a drop-down list tree

Source: Internet
Author: User

: Code: [csharp] using System. data; using System. web. UI. webControls; // <summary> // generate a drop-down list tree based on the DataTable // </summary> public class DropDownListHelp {private string gridline; private DataTable dt; public DropDownListHelp () {// TODO: add the constructor logic here // <summary> // generate a tree-like drop-down menu based on the Datatable /// </summary> /// <param name = "datatable "> </param> /// <param name =" parentKeyField "> key field of the parent node </param> /// <para M name = "parentKey"> upper-level node value </param> /// <param name = "keyField"> key field of the current node </param> /// <param name = "sortString"> sort string </param> // <param name = "ddl"> DownList </param> public void createDropDownTree (DataTable datatable, string parentKeyField, string parentKey, string keyField, string textField, string sortString, DropDownList ddl) {dt = datatable; ddl. items. add (new ListItem ("", ""); addChildItems (parentKe YField, parentKey, keyField, textField, sortString, ddl );} /// <summary> /// recursively generated Tree node /// </summary> /// <param name = "parentKeyField"> key field of the parent node </param>/ // <param name = "parentKey"> upper-level node value </param> // <param name = "keyField"> key field of the current node </param> // <param name = "sortString"> sort string </param> // <param name = "ddl"> DownList Control </param> /// <returns> </returns> private void addChildItems (string parentKeyField, string ParentKey, string keyField, string textField, string sortString, DropDownList ddl) {DataView dv = new DataView (dt, parentKeyField + "= '" + parentKey + "'", sortString, DataViewRowState. currentRows); int a = dv. count; if (dv. count = 0) {return ;}for (int I = 0; I <a; I ++) {gridline = ""; dv. rowFilter = parentKeyField + "= '" + parentKey + "'"; dv. sort = sortString; getTreeLine (parentKeyField, Dv [I] [parentKeyField]. toString (), keyField, dv [I] [keyField]. toString (), sortString); dv. rowFilter = parentKeyField + "= '" + parentKey + "'"; dv. sort = sortString; ddl. items. add (new ListItem (gridline + (I = a-1? "Plaintext": "plaintext") + dv [I] [textField]. toString (), dv [I] [keyField]. toString (); addChildItems (parentKeyField, dv [I] [keyField]. toString (), keyField, textField, sortString, ddl);} dv. dispose ();} /// <summary> /// trace the connection line of the Spanning Tree /// </summary> /// <param name = "parentKeyField"> key field of the parent node </param> /// <param name = "parentKey"> upper-level node value </param> /// <param name = "keyField"> key field of the current node </param> /// <param name = "nodeKey"> current node value </param> /// <param name = "sortString"> sort string </param> /// <returns> </ returns> private void getTreeLine (string parentKeyField, string parentKey, string keyField, string nodeKey, string sortString) {// select the parent node DataView dv = new DataView (dt, keyField + "= '" + parentKey + "'", sortString, DataViewRowState. currentRows); if (dv. count> 0) {// select the same level node dv of the parent node. rowFilter = parentKeyField + "= '" + dv [0] [parentKeyField]. toString () + "'"; dv. sort = sortString; for (int j = 0; j <dv. count; j ++) {if (dv [j] [keyField]. toString () = parentKey) {if (j = dv. count-1) {gridline = "" + gridline;} else {gridline = "break" + gridline ;}} getTreeLine (parentKeyField, dv [0] [parentKeyField]. toString (), keyField, dv [0] [keyField]. toString (), sortString);} dv. dispose () ;}} call: [csharp] DropDownListHelp ddlHelper = new DropDownListHelp (); ddlHelper. createDropDownTree (dt, "parentID", "1", "ID", "Text", "Text asc", DropDownList1 );

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.