C# 启用Multiselect时,使用ObjectDataSource绑定Listbox/CheckedListBox的SelectedValue
我目前没有以最佳方式解释我的问题的代码。所以可能会有一些语法错误,可能会留下一些与数据源相关的绑定 情景 我有一个Customer类,它包含一些属性 例如 我使用了一个函数GetCustomer返回集合C# 启用Multiselect时,使用ObjectDataSource绑定Listbox/CheckedListBox的SelectedValue,c#,asp.net,gridview,C#,Asp.net,Gridview,我目前没有以最佳方式解释我的问题的代码。所以可能会有一些语法错误,可能会留下一些与数据源相关的绑定 情景 我有一个Customer类,它包含一些属性 例如 我使用了一个函数GetCustomer返回集合 public Collection<Customer> GetCustomer(); 此函数使用ObjectDataSource控件与GridView绑定 i、 e 该网格再次绑定到ObjectDataSource控件,该控件使用GetCustomer函数绑定网格 问题 是我想显示
public Collection<Customer> GetCustomer();
此函数使用ObjectDataSource控件与GridView绑定
i、 e
该网格再次绑定到ObjectDataSource控件,该控件使用GetCustomer函数绑定网格
问题
是我想显示/更新列表框控件中绑定的所有选定项。
i、 e.如果列表框有10项,BookCollection包含3项。
然后,这3个项目应显示为选中。当用户更改选择时,这些应该反映在集合本身中。就我个人而言,我不会在ASP标记中执行此类操作。因此,我不确定是否可以绑定完整的书籍列表,并单独在标记中为每个客户选择书籍-当然,SelectedValue属性不是实现这一点的方法 下面是我将如何做这样的事情: 标记:
<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>
这段代码并不漂亮,需要填写一些详细信息,例如BooksPurchased对象的结构,但它应该让您找到显示每个客户所选书籍的正确路径
当用户在列表框中选择不同的项目时,管理添加和删除书籍会有点复杂,每个选项都取决于实现细节,例如:如何存储客户(如果有的话)?您是立即更新数据库,还是在用户单击提交按钮之前缓存更改?。如果你能提供更多关于这一部分的细节,我可能也能在这方面提供帮助
<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>
<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>
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;
}
}
}
}