启用Multiselect时使用ObjectDataSource绑定Listbox / CheckedListBox的SelectedValue [英] Binding Listbox/CheckedListBox's SelectedValue using ObjectDataSource when Multiselect is enabled

查看:212
本文介绍了启用Multiselect时使用ObjectDataSource绑定Listbox / CheckedListBox的SelectedValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我没有代码以最好的方式解释我的问题。所以可能会有一些语法错误,可能会留下一些与数据源相关的绑定。



场景。

作为包含一些属性的客户的类



例如。

  class Customer 
{
int CustomerId {get; set;} //主键
int age {get; set;}
字符串名称{get; set;}
收藏<图书购买> booksCollection {get; set;}
}

我使用了一个函数Ge​​tCustomer返回集合

  public集合< Customer> GETCUSTOMER(); 

这个函数和使用ObjectDataSource控件的GridView绑定在一起。

 < asp:GridView DataKey =CustomerId> 
<栏>
< asp:TemplateField>
< ItemTemplate><%#Eval('age')%>< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField>
< ItemTemplate><%#Eval('name')%>< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField>
< ItemTemplate>
< asp:Listbox DataSourceId =availableBooksSelectedValue ='<%#Bind(booksCollection)%>'/>
< asp:ObjectDataSource SelectMethod =GetBooksCollectionTypeName =Books>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>

该网格再次绑定到一个ObjectDataSource控件,该控件使用GetCustomer()函数绑定网格。 / b>

问题
是我想要显示/更新和列表框控件中绑定的所有选定项目。
即If Listbox有10个项目,而BookCollection包含3个项目。
然后这3个项目应该显示为选中状态。当用户chages选择这些应该反映在集合本身。

在ASP标记中。因此,我不确定是否可以绑定完整的图书清单,并单独为每个客户选择图书 - 当然,SelectedValue属性并不是实现此目的的方式。



以下是我如何做这样的事情:



标记:

 < asp:GridView ID =customersDataKey =CustomerId> 
<列>
< asp:TemplateField>
< ItemTemplate><%#Eval('age')%>< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField>
< ItemTemplate><%#Eval('name')%>< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField>
< ItemTemplate>
< asp:Listbox ID =booksDataSourceId =availableBooks/>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>

代码隐藏:

  protected override OnInit(EventArgs e)
{
base.OnInit(e);

customers.RowDataBound + = new GridViewRowEventHandler(customers_RowDataBound);
}

void customers_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.RowType == DataControlRowType.DataRow)
{
Customer currentCustomer =(Customer)e.Row.DataItem;
Listbox books =(ListBox)e.Row.FindControl(books);

books.DataSource = GetBooksCollection();
books.DataBind();

foreach(Books购买currentCustomer.booksCollection中的currentBook)
{
if(books.Contains(currentBook))
{
books.Selected = true;
}
}
}
}

代码并不漂亮,并且需要填充一些细节(例如BooksPurchased对象的结构),但它应该让您找到显示每个客户所选图书的正确路径。



当用户在列表框中选择不同的项目时,管理添加和删除书籍会更复杂一些,每个选项都取决于实施细节(例如:如何存储客户,如果有的话)更新数据库,或缓存更改,直到用户单击提交按钮?)。如果您可以提供关于此部分的更多细节,我也可以提供帮助。


I do not have code at the moment for explaining my question in the best way. So there might be some syntax mistakes, might leave some datasource related binding.

Scenario.

I have a class as Customer that contains some of the properties

eg.

class Customer
{
          int CustomerId{get;set;} //primary key
          int age{get;set;}
          string name{get;set;}
          Collection<BooksPurchased> booksCollection{get;set;}
} 

I used a function say GetCustomer() which returns Collection

public Collection<Customer> GetCustomer();

This function is bound with GridView using ObjectDataSource control.

i.e.

<asp:GridView DataKey="CustomerId">
<columns>
<asp:TemplateField>
     <ItemTemplate><%# Eval('age') %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
     <ItemTemplate><%# Eval('name') %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
     <ItemTemplate>
             <asp:Listbox DataSourceId="availableBooks" SelectedValue='<%# Bind("booksCollection") %>' />
             <asp:ObjectDataSource SelectMethod="GetBooksCollection" TypeName="Books">   
     </ItemTemplate>
</asp:TemplateField>
   </Columns>
</asp:GridView>

This Grid is again binded to a ObjectDataSource control which tables GetCustomer() function to bind the grid.

Problem is I want to display/Update and all the selected items binded in Listbox control. i.e. If Listbox has 10 items and booksCollection contains 3 items. Then these 3 items should be displayed as selected. And when user chages selection these should get reflected in the collection itself.

解决方案

Personally, I stay away from performing this sort of operation in the ASP markup. Because of that, I'm not sure if you can bind your full list of books and select the books for each customer in the markup alone -- certainly, the SelectedValue property is not the way to do this.

Here's how I would do something like this:

Markup:

<asp:GridView ID="customers" DataKey="CustomerId">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate><%# Eval('age') %></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate><%# Eval('name') %></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Listbox ID="books" DataSourceId="availableBooks" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code-behind:

protected override OnInit(EventArgs e)
{
    base.OnInit(e);

    customers.RowDataBound += new GridViewRowEventHandler(customers_RowDataBound);
}

void customers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Customer currentCustomer = (Customer) e.Row.DataItem;
        Listbox books = (ListBox) e.Row.FindControl("books");

        books.DataSource = GetBooksCollection();
        books.DataBind();

        foreach (BooksPurchased currentBook in currentCustomer.booksCollection)
        {
            if (books.Contains(currentBook))
            {
                books.Selected = true;
            }
        }
    }
}

This code isn't pretty, and needs some details filled in (such as the structure of the BooksPurchased object), but it should get you on the right path to displaying each customer's selected books.

It's a bit more complicated to manage adding and removing books when the user selects different items in the ListBox, and each option depends on implementation details (for instance: how are you storing the customer, if at all? Are you instantly updating the database, or caching changes until the user clicks a submit button?). If you can provide some more detail about this part, I might be able to help on it, too.

这篇关于启用Multiselect时使用ObjectDataSource绑定Listbox / CheckedListBox的SelectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆