1

我不知道我是否可以在这里发布这样一个特定的问题。

场景是我有一个与类别有多对多关系的产品。意味着一个产品可能属于多个类别,一个类别可能有多个产品。现在在表单上,​​我提供了一个下拉列表以及一个显示添加另一个类别的按钮,当我单击该按钮时,另一个下拉列表应出现在第一个下拉列表下方。并且必须从下拉项目中删除上一个下拉列表的选定项目。

下面是我的下拉中继器代码和提到的两个按钮。

    protected void btnAddAnotherCategory_Click(object sender, EventArgs e)
    {
        List<int> selectedIndices = new List<int>();
        foreach (RepeaterItem item in rptCategories.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                DropDownList ddlCategory = (DropDownList)item.FindControl("ddlCategory");
                int selectedIndex = ddlCategory.SelectedIndex;
                selectedIndices.Add(selectedIndex);
            }
        }
        ViewState["objSelectedIndices"] = selectedIndices;
        rptCategories_DataSource("add", false);
    }

    protected void btnRemoveCategory_Click(object sender, EventArgs e)
    {
        List<int> selectedIndices = new List<int>();
        foreach (RepeaterItem item in rptCategories.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                DropDownList ddlCategory = (DropDownList)item.FindControl("ddlCategory");
                int selectedIndex = ddlCategory.SelectedIndex;
                selectedIndices.Add(selectedIndex);
            }
        }
        ViewState["objSelectedIndices"] = selectedIndices;
        rptCategories_DataSource("remove", false);
    }

    protected void rptCategories_DataSource(string editCommand, bool pageLoad)
    {
        switch (editCommand)
        {
            case "add":
                if (ViewState["categoriesCount"] == null)
                {
                    List<Category> count = new List<Category>();
                    count.Add(new Category());
                    ViewState["categoriesCount"] = count;
                }
                if (!pageLoad)
                {
                    List<Category> count = (List<Category>)ViewState["categoriesCount"];
                    count.Add(new Category());
                    ViewState["categoriesCount"] = count;
                }
                List<Category> objCategories = (List<Category>)ViewState["categoriesCount"];
                objCategories = objCategories.Where(x => x.StatusID != 3).ToList();
                rptCategories.DataSource = objCategories;
                rptCategories.DataBind();
                break;
            case "remove":
                if (((List<Category>)ViewState["categoriesCount"]).Count > 1)
                {
                    List<Category> count = (List<Category>)ViewState["categoriesCount"];
                    count.Remove(count.Last());
                    count = count.Where(x => x.StatusID != 3).ToList();
                    ViewState["categoriesCount"] = count;
                    rptCategories.DataSource = count;
                    rptCategories.DataBind();
                }
                break;
        }
    }

    protected void rptCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Category objCategory = (Category)e.Item.DataItem;

        DropDownList ddlCategory = (DropDownList)e.Item.FindControl("ddlCategory");
        ddlCategory.DataSource = new CategoryBLL().GetAllCategoriesWithStatus();
        ddlCategory.DataTextField = "Name";
        ddlCategory.DataValueField = "ID";
        ddlCategory.DataBind();

        if (objCategory.CategoryID != null)
            ddlCategory.SelectedValue = objCategory.CategoryID.ToString();

        if (ViewState["objSelectedIndices"] != null)
        {
            List<int> objSelectedIndices = (List<int>)ViewState["objSelectedIndices"];
            if (objSelectedIndices.Count > e.Item.ItemIndex)
                ddlCategory.SelectedIndex = objSelectedIndices.ElementAt(e.Item.ItemIndex);
        }

        if (e.Item.ItemIndex < ((List<Category>)rptCategories.DataSource).Count - 1)
        {
            ddlCategory.Enabled = false;
        }

        btnAddAnotherCategory.Visible = true;
        btnRemoveCategory.Visible = true;
        if (rptCategories.Items.Count < 1)
        {
            btnRemoveCategory.Visible = false;
        }
        if (rptCategories.Items.Count >= new CategoryBLL().GetAllCategoriesWithStatus().Count - 1)
        {
            btnAddAnotherCategory.Visible = false;
        }
    }

代码中提到的 StatusID 是为了确定已删除的产品,因为我实际上并没有删除产品,而只是设置了它的 StatusID

现在的问题是我的代码在上述语句中运行良好,但正如您所见,它不会从下拉列表中删除选定的项目。每次都需要从单个表中填充下拉列表。棘手的部分是删除先前重复项下拉列表中的选定项。任何解决方案都将受到欢迎。

4

0 回答 0