zlb

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_);
        }
 
    }
 

posted on 2011-03-28 11:22  zlb  阅读(523)  评论(0编辑  收藏  举报

导航