无法从“字符串”转换为“System.Web.UI.WebControls.TreeNode”
本文关键字:字符串 Web UI TreeNode WebControls System 转换 | 更新日期: 2023-09-27 18:33:40
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));