【发布时间】:2012-05-18 20:51:48
【问题描述】:
不知道为什么我会在 Webpart 中为 treeView 收到此错误,说实话可能是一个逻辑错误,
tree.Nodes.Add(groupNode);
为什么会这样说:S
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(new TreeNode(UserPair.Value));
}
}
}
tree.Nodes.Add(groupNode);
this.Controls.Add(tree);
}
catch (Exception)
{
//loggingit
}
}
干杯
【问题讨论】:
-
这与您的问题无关,但您目前正在
foreach循环中将新的TreeNode重新分配给您的groupNode。我无法想象这会产生你想要的结果。 -
我不太确定这个 treeNode tbh 在做什么,你可以叫我菜鸟
-
不用担心。我们都去过那里!
标签: c# .net treeview web-parts nodes