对 FormView 控件中的多选列表框进行数据绑定

发布于 2024-08-28 21:21:44 字数 557 浏览 5 评论 0原文

我在 FormView 中有一个多选列表框。我不知道如何为其设置数据绑定。
我可以很好地填充列表框。 如果列表框是单个选择,我可以使用 SelectValue='<%#Bind("col")%>' ,效果很好。 我可以绑定多选列表框吗? 我尝试通过处理列表框的 DataBinding 事件并在适当的项目上设置选定的属性来手动进行 DataBinding。不幸的是,在 DataBinding 事件期间列表框中没有任何项目。 我发现的最接近的事情是保存确定在 DataBinding 期间应选择哪些项目的值,然后在 FormViews DataBinding 事件中使用该值来选择正确的项目,这对我来说似乎是一种黑客攻击。

有更好的办法吗?


编辑:

为了澄清我当前正在做什么...

我正在使用 FormViews 的 ItemCreated 事件来保存 FormView 的 DataItem。 然后在 FormView 的 DataBound 事件中,我找到列表框并手动设置所选项目。 我必须像这样保存值似乎不对,我认为有一种更正确的方法可以做到这一点,但我只是看不到。

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='<%#Bind("col")%>' 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.

Is there a better way?


EDIT:

To clarify what I am currently doing...

I am using the FormViews's ItemCreated event to save the FormView's DataItem.
Then in the FormView's DataBound event I find the listbox and manually set the selected items.
It doesn't seem right that I have to save the value like this and I assume there is a more correct way to do this that I just can't see.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可可 2024-09-04 21:21:44

您是否尝试过 ItemDataBound 事件?

Did you try the ItemDataBound event?

他不在意 2024-09-04 21:21:44

当我过去完成此操作时,我设置了列表框的数据源,绑定它,然后迭代列表框中的项目列表,并将与我的条件或值匹配的每个项目的 .Selected 设置为 true。我不确定它是最有效的,但对于我处理过的小列表来说,它确实工作得很好。

When I've done this in the past I've set the data source of the listbox, bind it, then iterate through the list of items in the listbox and set the .Selected to true for each item that matches my criteria or values. I'm not sure it's the most efficient, but for the small lists I've dealt with it certainly worked fine.

给不了的爱 2024-09-04 21:21:44

我最终所做的就是完全放弃 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,但那是另一天的故事了。

What I ended up doing was abandoning the ListBox all together and I settled on a TreeView, as it actually served my purposes better.

Before that though I did write this function to select the current items and I called it from Page_PreRender because binding is complete then, and I could get the controls I needed.

    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;
                    }
                } 
            }
        }
    }

And then when I need to update from the ListBox I called the following code from the FormView.ItemUpdating event.

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();
                }
            }
        }
    }
}

This was really bad perfomance-wise, but it did work. Maybe I could have improved it if I compiled the linq query, but I never got that far. I understand from your comment above that you might have settled on your own workaround to the problem so I am only adding this answer in case it may help future lost souls such as myself.

In the end a TreeView was a better tool for me so I didn't need to do this anyway. Although that began a new adventure, as you can't easily bind a TreeView to a LinqDataSource, but that is a tale for another day.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文