Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6671855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:26:24+00:00 2026-05-26T03:26:24+00:00

I do not have code at the moment for explaining my question in the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:26:25+00:00Added an answer on May 26, 2026 at 3:26 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some not understanding actions from gnu clisp Suppose, I have some code
I have some code which I did not originally create that uses _beginthreadex and
I have some code here which works perfectly in firefox but not in chrome
I have some code at the moment to draw a graph based on the
sorry but I do not have the actual code with me, but I will
Conditions: Windows 98 SE WMI not available I have code that looks like this,
I usually do not have difficulty to read JavaScript code but for this one
Motorola phones do not have Verisign/Thawte root certificates for code signing. They only embed
I'm not sure why my code is not working.. I have code in vba
In the following code, it seems class C does not have access to A's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.