TreeView的逻辑错误

时间:2012-05-10 11:00:44

标签: c# asp.net .net sharepoint treeview

当我尝试将树视图控件添加到webpart时,它会给我这个例外,

  

“指数超出范围。必须是非负数且小于规模   的集合。参数名称:索引“

   if (groupNode != null)
    {
        tree.Nodes.Add(groupNode);
        this.Controls.Add(tree);
    } 

然而,当我调试时,只有当我尝试将控件添加到webpart时它才会获得异常,这是代码,

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

        try
        {
            TreeView tree = new TreeView();
            TreeNode groupNode = default(TreeNode);
            List<string> GroupList = new List<string>();
            List<KeyValuePair<string, string>> UserList = new List<KeyValuePair<string, string>>();
            List<string> IndividualUserList = new List<string>();

            GroupList.Clear();
            UserList.Clear();
            IndividualUserList.Clear();

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

                if (!user.IsDomainGroup &&  groupName != "" && groupName != "System Account")
                    IndividualUserList.Add(groupName);

                else if (user.IsDomainGroup && !string.IsNullOrEmpty(groupName) && 
                    Directory.DoesGroupExist(groupName))
                {
                    GroupList.Add(groupName);
                    foreach (ADUser member in Directory.GetUsersFromGroup(groupName))
                    {
                        if (member != null && !string.IsNullOrEmpty(member.UserName))
                        {
                            string MemberName = member.UserName.ToString().Replace(".", " ");
                            UserList.Add(new KeyValuePair<string, string>(groupName, MemberName));
                        }
                    }
                }
            }

            IndividualUserList.Sort();

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

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

            if (groupNode != null)
            {
                tree.Nodes.Add(groupNode);
                this.Controls.Add(tree);
            } 
        }
        catch (Exception ex)
        {
            //loggin it
        }
    }

0 个答案:

没有答案