ASP.NET在转发器中删除DropDownList项?

时间:2013-04-14 16:07:09

标签: c# asp.net html-select asprepeater

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

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

下面是我的下拉列表转发器代码和上面提到的两个按钮。

    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

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

0 个答案:

没有答案