开发者

Binding a TreeView control

I am using ASP.NET and C# to bind a tree view control in my application. The following code is used in WinForms, but开发者_开发知识库 it's not working in WebForms. Can someone help to convert this to WebForms?

private TreeNode AddNode(TreeNode node, string key)
{
    if (node.Nodes.ContainsKey(key))
    {
        return node.Nodes[key];
    }
    else
    {
        return node.Nodes.Add(key, key);
    }
}

I want to implement the same logic. In WebForms we do not have method node.nodes, or node.nodes.Containskey().


In tree web control there is Value property, which can be used to store application data. But you have to manage it by yourself. Your code should look like this:

private TreeNode AddNode(TreeNode node, string key)
{
  val child = node.ChildNodes.Cast<TreeNode>().FirstOrDefault(_ => _.Value == key);
  if(child != null)
     return child;

  child = new TreeNode(key, key);
  node.ChildNodes.Add(child);
  return child;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜