簡體   English   中英

無法從“字符串”轉換為“ System.Web.UI.WebControls.TreeNode”

[英]cannot convert from 'string' to 'System.Web.UI.WebControls.TreeNode'

鄧諾(Dunno)為什么會變得愚蠢或更像是感覺更少的錯誤,通常我們將字符串添加到treeNode,然后為什么不在此代碼中,

groupNode.ChildNodes.Add(UserPair.Value);   

(為什么不 ?)

    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        try
        {
            int Index = 0;
            TreeView tree = new TreeView();
            TreeNode groupNode; 
            Dictionary<int, string> GroupList = new Dictionary<int, string>();
            Dictionary<int, string> UserList = new Dictionary<int, string>();
            List<string> IndividualUserList = new List<string>();

            foreach (SPUser user in SPContext.Current.Web.Users)
            {
                string groupName = FormatUserLogin(user.Name);

                if (groupName != "" && groupName != "System Account")
                    IndividualUserList.Add(groupName);
                else if (user.IsDomainGroup && !string.IsNullOrEmpty(groupName) && 
                    Directory.DoesGroupExist(groupName))
                {
                    Index++;
                    GroupList.Add(Index, groupName);
                    List<ADUser> adUsers = Directory.GetUsersFromGroup(groupName);

                    foreach (ADUser member in adUsers)
                    {
                        if (member != null && !string.IsNullOrEmpty(member.DisplayName))
                            UserList.Add(Index, member.DisplayName);
                    }
                }
            }

            IndividualUserList.Sort();

            foreach (string Item in IndividualUserList)
            {
                groupNode = new TreeNode(Item);
            }

            foreach (KeyValuePair<int, string> GroupPair in GroupList)
            {
                groupNode = new TreeNode(GroupPair.Value);
                foreach (KeyValuePair<int, string> UserPair in UserList)
                {
                    if (UserPair.Key == GroupPair.Key)
                        groupNode.ChildNodes.Add(UserPair.Value);
                }
            }

            tree.Nodes.Add(groupNode);

            this.Controls.Add(tree);
        }
        catch (Exception)
        {
            //loggingit
        }
    }

我以為treeNodes可以向它們添加字符串值,如果我的代碼中有邏輯錯誤或錯誤,請讓我知道。

干杯

回答

if (UserPair.Key == GroupPair.Key)
                    {
                        TreeNode userNode = new TreeNode(UserPair.Value);
                        groupNode.ChildNodes.Add(userNode);
                    }

groupNode.ChildNodes是一個TreeNodeCollection 您只能將TreeNode類型的對象添加到集合中。 更改此行:

groupNode.ChildNodes.Add(UserPair.Value);

至:

groupNode.ChildNodes.Add(new TreeNode(UserPair.Value));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM