1

そのような特定の問題をここに投稿することが許可されているかどうかはわかりません。

シナリオは、カテゴリと多対多の関係を持つ製品があることです。1 つの製品が複数のカテゴリに属し、1 つのカテゴリに複数の製品が含まれる可能性があることを意味します。フォームで、別のカテゴリを追加するというボタンとともにドロップダウンを提供しました。そのボタンをクリックすると、最初のドロップダウンの下に別のドロップダウンが表示されます。また、前のドロップダウンで選択したアイテムをドロップダウン アイテムから削除する必要があります。

以下は、ドロップダウンのリピーターと言及された2つのボタンの私のコードです。

    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 ドロップダウンリストで選択したアイテムを削除することです。どんな解決策も歓迎されます。

4

0 に答える 0