ASP.Net DropDownList绑定树状结构
获取父节点 开始调用 listSource 是List<> 数据集合 (有父子关系)
foreach (ModelInfo Item in listSource)
{
if (Item.ParentID == 0)
{
ListItem li = new ListItem();
li.Text = Item.Item;
li.Value = Item.ID.ToString();
YouDropDownList.Items.Add(li);
BindDDLData(YouDropDownList, listSource, Item, "├à─¤─¤");
}
}
递归绑定
private void BindDDLData(DropDownList Ddl, List<ModelInfo> List, ModelInfo CurrentInfo, string separator)
{
var quer = (from c in List
where c.ParentID == CurrentInfo.ID
select c);
foreach (var item in quer)
{
ListItem li = new ListItem();
li.Text = separator + item.Item;
li.Value = item.ID.ToString();
Ddl.Items.Add(li);
string separator_ = separator + "─¤─¤─¤";
BindDDLData(Ddl, List, item, separator_);
}
}