开发者

Binding Listbox/CheckedListBox's SelectedValue using ObjectDataSource when Multiselect is enabled

开发者 https://www.devze.com 2023-04-07 17:27 出处:网络
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.

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><%#开发者_如何学Go 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消