C#递归在dropdownlist显示树状
/// <summary>
/// 绑定文件分类
/// </summary>
public void bind()
{
//获取数据集
DataSet ds = new DataSet();
ds = ec.ExecuteSelectCmmond("select * from t_Column where U_UserID=" + HttpContext.Current.Request.Cookies["NameID"].Value + "", ds);
BindingCWAList(DropDownList1, ds);
}
private void BindingCWAList(DropDownList ddlID, DataSet ds) //ddlID是DropDownList控件的ID
{
DropDownList1.Items.Clear();
//此处创建顶极分类,Value=0(如不需要,可以删除此行)
ddlID.Items.Add(new ListItem("请选择目录", "0"));
InitList(ddlID, 0, ds, "");
}
private void InitList(DropDownList ddlID, int parentID, DataSet catagoryDS, string indent)
{
//Select后边的是DataSet里面的列名
System.Data.DataRow[] currRows = catagoryDS.Tables[0].Select("ColumnFatherld=" + parentID.ToString(), "ColumnFatherld ASC");
int count = currRows.Length;
DataRow catagoryRow;
for (int i = 0; i < count; i++)
{
catagoryRow = currRows[i];
System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem(indent + catagoryRow ["ColunmnName"].ToString(), catagoryRow["ColumnId"].ToString());
ddlID.Items.Add(item);
InitList(ddlID, Int32.Parse(catagoryRow["ColumnId"].ToString()), catagoryDS, indent + "……");
}
}
补充:软件开发 , C# ,