asp.net C # generate Drop-down list Tree Implementation Code _ Practical Tips

Source: Internet
Author: User
Effect Chart:

Code:
Copy Code code as follows:

Using System.Data;
Using System.Web.UI.WebControls;
<summary>
Generate a Drop-down list tree from a DataTable
</summary>
public class Dropdownlisthelp
{
private string gridline;
Private DataTable DT;
Public Dropdownlisthelp ()
{
//
TODO: Add constructor logic here
//
}
<summary>
Generate a tree Drop-down menu from a DataTable
</summary>
<param name= "DataTable" ></param>
<param name= "Parentkeyfield" > Parent node key field </param>
<param name= "Parentkey" > Parent node Value </param>
<param name= "Keyfield" > This node key field </param>
<param name= "sortstring" > Sort string </param>
<param name= "DDL" >DownList</param>
public void Createdropdowntree (DataTable DataTable, String Parentkeyfield, String Parentkey, String Keyfield, String text Field, String sortstring, DropDownList DDL)
{
DT = DataTable;
Ddl. Items.Add (New ListItem ("", ""));
Addchilditems (Parentkeyfield, Parentkey, Keyfield, TextField, sortstring, DDL);
}
<summary>
Recursive spanning tree node
</summary>
<param name= "Parentkeyfield" > Parent node key field </param>
<param name= "Parentkey" > Parent node Value </param>
<param name= "Keyfield" > This node key field </param>
<param name= "sortstring" > Sort string </param>
<param name= "DDL" >downlist controls </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?) "┗": "Out") + Dv[i][textfield]. ToString (), Dv[i][keyfield]. ToString ()));
Addchilditems (Parentkeyfield, Dv[i][keyfield). ToString (), Keyfield, TextField, sortstring, DDL);
}
Dv. Dispose ();
}
<summary>
A connection line that backtracking spanning a tree
</summary>
<param name= "Parentkeyfield" > Parent node key field </param>
<param name= "Parentkey" > Parent node Value </param>
<param name= "Keyfield" > This node key field </param>
<param name= "Nodekey" > This 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 Parent Layer Node
DataView dv = new DataView (DT, Keyfield + "= '" + Parentkey + "" ", Sortstring, DataViewRowState.CurrentRows);
if (DV. Count > 0)
{
Select parent node Sibling node
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 = "┃" + gridline;
}
}
}
Gettreeline (Parentkeyfield, Dv[0][parentkeyfield). ToString (), Keyfield, Dv[0][keyfield]. ToString (), sortstring);
}
Dv. Dispose ();
}
}

Call:
Copy Code code 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.