ASP. net c # generate the drop-down list tree implementation code

Source: Internet
Author: User

:

Code: Copy codeThe Code is as follows: 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 drop-down menu based on the Datatable
/// </Summary>
/// <Param name = "datatable"> </param>
/// <Param name = "parentKeyField"> keyword 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 </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 (parentKeyField, parentKey, keyField, textField, sortString, ddl );
}
/// <Summary>
/// Recursive Tree node generation
/// </Summary>
/// <Param name = "parentKeyField"> keyword 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? "Outputs": "outputs") + 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"> keyword 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 parent node at the same level
Dv. 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 = "finished" + gridline;
}
}
}
GetTreeLine (parentKeyField, dv [0] [parentKeyField]. ToString (), keyField, dv [0] [keyField]. ToString (), sortString );
}
Dv. Dispose ();
}
}

Call:Copy codeThe Code is as follows: 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.