选择任何项目后,无法在树视图中添加节点索引必须在List的范围内。参数名称:index

时间:2011-06-30 07:11:03

标签: asp.net-mvc c#-4.0 treeviewitem

当我在树形视图中添加节点后,选择任何节点一段时间它的添加和一些时间项不添加并给出错误索引必须在List的范围内。参数名称:index

我关注的代码是

public void AddWorkflowItem()
        {
            DeletePageNoFromContentTreeView();
            string ToolTips = null;
            if (baseCase.LockUser == null)
            {
                View.ResultOperation.Message =
                    "Case has been unlocked by ACR Production Specialist. System is unable to save changes.";
                View.ResultOperation.IsSuccessfully = false;
                return;
            }
            if (baseCase.LockUser.Id != AuthenticationService.CurrentUser.Id)
            {
                View.ResultOperation.Message =
                    new StringBuilder("Case has been locked by ")
                        .Append(baseCase.LockUser.Profile.Degree + " ")
                        .Append(baseCase.LockUser.Profile.FirstName + " ")
                        .Append(baseCase.LockUser.Profile.LastName + ". System is unable to save changes.")
                        .ToString();
                View.ResultOperation.IsSuccessfully = false;
                return;
            }
            CipCaseWorkflowItem newItem = null;
            ISet<CipCaseWorkflowItem> list = GetCaseWorkflow();
            CaseContentItemType selectedItem =
                (CaseContentItemType)
                Enum.Parse(typeof(CaseContentItemType), View.AddCaseContentItemTypeList.SelectedValue);
            ContentElementType selectedTextItem = ContentElementType.None;
            switch (selectedItem)
            {
                case CaseContentItemType.BaseCaseTextElement:
                    selectedTextItem =
                        (ContentElementType)
                        Enum.Parse(typeof(ContentElementType), View.AddCaseContentSubItemTypeList.SelectedValue);
                    //GetContentElementType(View.AddCaseContentSubItemTypeList.SelectedValue);//
                    newItem = new CipTextElementScriptItem(cipCase, selectedTextItem);
                    newItem.Type = selectedItem;
                    newItem.IsEmpty = true;
                    break;
                case CaseContentItemType.TemplatedItem:
                    CaseContentTemplatedItemType selectedTemplateItem =
                        (CaseContentTemplatedItemType)
                        Enum.Parse(typeof(CaseContentTemplatedItemType),
                                   View.AddCaseContentSubItemTypeList.SelectedValue);
                    //newItem = new CipTemplatedScriptItem(cipCase, selectedTemplateItem);
                    newItem = CreateCipWorkflowTemplatedItem(cipCase, selectedTemplateItem);
                    newItem.IsEmpty = false;
                    break;
                case CaseContentItemType.Question:
                    newItem = new CipQuestionScriptItem(cipCase);
                    ((CipQuestionScriptItem)newItem).ColumnsNo = 1;
                    newItem.Type = selectedItem;
                    if (View.AddActiveView == CaseContentAddActiveView.NoQuestions)
                    {
                        newItem.IsEmpty = true;
                        ToolTips = string.Empty;
                    }
                    else
                    {
                        int questionId = Int32.Parse(View.AddQuestionList.SelectedValue);
                        QuestionElement question = FindQuestionById(questionId);
                        ((CipQuestionScriptItem)newItem).QuestionElement = question;
                        newItem.IsEmpty = false;
                        ToolTips = question.GetToolTip();
                    }
                    break;
                case CaseContentItemType.CaseCredits:
                case CaseContentItemType.PageBreak:
                case CaseContentItemType.CIPCMEGraphic:
                case CaseContentItemType.CaseAuthours:
                case CaseContentItemType.CMEInfo:
                case CaseContentItemType.PostTest:
                case CaseContentItemType.EmptyLine:
                case CaseContentItemType.HorizontalLine:
                    newItem = new CipCaseWorkflowItem(cipCase, selectedItem);
                    newItem.IsEmpty = false;
                    break;
                    case CaseContentItemType.Image:
                    newItem = new CipImageScriptItem(cipCase);
                    newItem.IsEmpty = false;                    
                    break;
                default:
                    throw new ApplicationException("Invalid context item type.");
            }
            TreeNode selectedNode = contentTreeView.SelectedNode;
            newItem.IsHidden = false;
            //TreeNode rootNode = contentTreeView.Nodes[0];
            if ((selectedNode != null) && !string.IsNullOrEmpty(selectedNode.Value))
            {
                CaseContentItemType itemType;
                ContentElementType textElementType;
                CaseContentTemplatedItemType templatedItemType;
                int id;
                string nodeId = selectedNode.Value;
                ParseNodeId(nodeId, out itemType, out textElementType, out templatedItemType, out id);
                CipCaseWorkflowItem current = FindSelectedItemIdById(id);
                int newItemSortField = current.SortField + 1;
                newItem.SortField = newItemSortField;
                IncreaseSortIndex(newItemSortField);
                cipCase.WorkflowItems.Add(newItem);
            }
            else
            {
                int maxIndex;
                GetMaxIndex(list, out maxIndex);
                maxIndex++;
                newItem.SortField = maxIndex;
                cipCase.WorkflowItems.Add(newItem);
            }
            if (selectedItem != CaseContentItemType.Question)
                FindAndAttachSingleItem(selectedItem, selectedTextItem, newItem, out ToolTips);
            baseCase.LastEditedDate = DateTime.Now;
            Ioc.Resolve<IWebSessionManager>().CurrentSession.Flush();
            TreeNode newNode = CreateTreeNode(newItem, null);
            if (ToolTips != null)
                newNode.ToolTip = AddContentItemStateToToolTip(newItem.IsHidden, ToolTips);
            else
                newNode.ToolTip = AddContentItemStateToToolTip(newItem.IsHidden, string.Empty);
            if (selectedNode != null)
                contentTreeView.Nodes.AddAt(newItem.SortField, newNode);
            else
                contentTreeView.Nodes.Add(newNode);
               SetWorkflowActionButtonState();
            if (selectedItem == CaseContentItemType.CaseCredits)
                SetupAddCombos();
            if (selectedItem == CaseContentItemType.Question)
                SetupSubItemCombo(selectedItem);

            DeletePageNoFromContentTreeView();
            SetupPagenumbersToContentTreeView();
            if (selectedItem == CaseContentItemType.Image)
            {
                SetupPageForSelectedNode(newNode);
                //View.AddActiveView = CaseContentAddActiveView.Images;
            }

            newNode.Select();
            SetupPageForSelectedNode(newNode);
            SetWorkflowActionButtonState();
        }

protected void IncreaseSortIndex(int sortIndex) {

foreach (CipCaseWorkflowItem item in cipCase.WorkflowItems)
    {
        if (item.SortField >= sortIndex)
        item.SortField++;
      }

}

错误就在这一行

  if (selectedNode != null)
                contentTreeView.Nodes.AddAt(newItem.SortField, newNode);

1 个答案:

答案 0 :(得分:0)

在调用DeletePageNoFromContentTreeView();

之前调用此函数
    protected void ResortWorkflowItems()
    {
        int i = 0;
        foreach (CipCaseWorkflowItem item in cipCase.WorkflowItems)
            item.SortField = i++;
        baseCase.LastEditedDate = DateTime.Now;
        Ioc.Resolve<IWebSessionManager>().CurrentSession.Flush();
    }