Populate listbox

Posted on 2009-02-10
Last Modified: 2012-08-14
This cant be that hard but how can I populate a listbox. I'm using LINQ but it shouldnt matter.

If I use listbox.Items.Add(whatever)..this works but i need the "key" value also.

if I do lisbox.Attributes.Add(key,value).. i dont get anything in the list box. This is what i have.
var siteNames = from sValues in dc.LSite

                          where !sValues.Id.Equals(Guid.Empty)

                          select sValues;

        foreach (var p in siteNames)



            lbSites.Attributes.Add(p.Code, p.Name);


Open in new window

Question by:Camillia
    LVL 19

    Accepted Solution

    Use this
    lbSites.Items.Add(new ListItem(p.Code, p.Name)); // first parameter si text, second parameter is value

    Open in new window

    LVL 7

    Author Comment

    let me try.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now