一千萬個為什麽

搜索

在FormView控件中數據綁定多選ListBox

I have a multiselect ListBox within a FormView. I can't figure out how to setup the databinding for it.
I can populate the listbox fine. If the listbox was a single select I could use say SelectValue='' and that works fine. Is there something that I can bind to for a multiselect listbox? I've tried manually DataBinding by handling the DataBinding event of the listbox and setting the selected property on the appropriate items. Unfortunately, there are no items in the listbox during the DataBinding event. The closest thing I've found is to save the value that determines what items should be selecting during DataBinding and then use that value in the FormViews DataBinding event to select the right items which seems like a hack to me.

有沒有更好的辦法?


編輯:

澄清我目前在做什麽......

我正在使用FormViews的ItemCreated事件來保存FormView的DataItem。 然後在FormView的DataBound事件中,我找到列表框並手動設置所選項。 我必須保存這樣的值並不正確,我認為有一種更正確的方法可以做到這一點,我看不到。

最佳答案

我最終做的是放棄ListBox,我決定使用 TreeView ,因為它實際上更好地滿足了我的目的。

在此之前,雖然我寫了這個函數來選擇當前項目,然後我從Page_PreRender調用它,因為綁定已經完成,我可以得到我需要的控件。

    protected void SelectCategories()
    {

        ListBox lb = (ListBox)fvProduct.FindControl("lstCategory");

        Product product = (Product)pdc.Products.Where(a => a.Sku == txtSku.Text).FirstOrDefault();
        var c = pdc.ProductCategories.Where(b => b.ProductId == product.ProductId);

        if (lb != null && lb.Items.Count > 0)
        {
            foreach (ProductCategory cat in c)
            {
                foreach (ListItem li in lb.Items)
                {
                    if (cat.CategoryId == Convert.ToInt32(li.Value))
                    {
                        li.Selected = true;
                    }
                } 
            }
        }
    }

然後,當我需要從ListBox更新時,我從FormView.ItemUpdating事件調用以下代碼。

protected void UpdateCategories()
{
    ListBox lb = (ListBox)fvProduct.FindControl("lstCategory");

    Product product = (Product)pdc.Products.Where(a => a.Sku == txtSku.Text).FirstOrDefault();

    if (lb != null && lb.Items.Count > 0)
    {
        foreach (ListItem li in lb.Items)
        {
            ProductCategory pc = new ProductCategory();
            pc = (ProductCategory)pdc.ProductCategories.Where(d => d.CategoryId == Convert.ToInt32(li.Value) && d.ProductId == product.ProductId).FirstOrDefault();

            if (pc == null)
            {
                if (li.Selected == true)
                {
                    //note: if li is selected but pc is null then insert new record .
                    pc = new ProductCategory();
                    pc.ProductId = product.ProductId;
                    pc.CategoryId = Convert.ToInt32(li.Value);
                    pdc.ProductCategories.InsertOnSubmit(pc);
                    pdc.SubmitChanges();
                }
            }
            else
            {
                if (li.Selected == false)
                {
                    //note: if li is not selected but pc is not null then remove record.
                    pdc.ProductCategories.DeleteOnSubmit(pc);
                    pdc.SubmitChanges();
                }
            }
        }
    }
}

這真的很糟糕,但確實有效。如果我編譯了linq查詢,也許我可以改進它,但我從來沒有這麽遠。我從你上面的評論中了解到,你可能已經找到了解決問題的方法,所以我只是添加了這個答案,以防它可能有助於未來失去靈魂,比如我自己。

最後,TreeView對我來說是一個更好的工具,所以無論如何我都不需要這樣做。雖然這開始了新的冒險,因為你無法輕松地將TreeView綁定到LinqDataSource,但這是另一天的故事。

轉載註明原文: 在FormView控件中數據綁定多選ListBox