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

  • Home
  • SEARCH
  • 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 141683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:48:57+00:00 2026-05-11T07:48:57+00:00

Preamble I’m asking this question because even though I’ve read through a lot of

  • 0

Preamble

I’m asking this question because even though I’ve read through a lot of ListView resources, I’m still not ‘getting’ it.

Background

I have a bunch of Foo‘s that have a list of items associated with them (known as Bar), and I’m pulling them from the Data Access/Business Logic layer as Dictionary that holds a Foo and its associated Bars. I’d like to spit these items out in on the Webpage into a ListView that holds the Foo.Name on the left, and the List<Bar> on the right in a dropdownlist. (Shown with my beautiful ASCII art below):

ListView

 ------------------------------------------------------------------ |           Name Of Item          |  DropDownList (of List<T>)   | |---------------------------------|  _____________________       | |                foo1             |  |     bar1      | v |       | |                                 |  |_______________|___|       |   ------------------------------------------------------------------ |                                 |  DropDownList (of List<T>)   | |                                 |  _____________________       | |                foo2             |  |     bar2      | v |       | |                                 |  |_______________|___|       | ------------------------------------------------------------------ 

Alright, here’s what’s going on. This is a ListView; The items are pulled from a database into a Dictionary<Foo, List<Bar>>. I’m trying to get the Key Value from the dictionary to show up under ‘Name of Item’, and am trying to get the `List<T> Bar’ to show up as a DropDownList on the right side of the ListView.

Class Diagrams

 -----------------          ----------------- |   Foo         |          |  Bar          | -----------------          ----------------- |  Id           |          |  ItemName     | |  Name         |          |  ItemValue    | |  BarID        |          |               | -----------------          ----------------- 

So to recap, I want to place the Dictionary.Key ‘Name’ into the left side of the ListView, and the Dictionary.Value (which happens to be a list) into a DropdownList on the right side.

So that, for every Key/Value pair, there’d be a Name and a dropdown list that would house each Key’s Value.

But I’m running into problems (obviously), and am hoping someone can tell me what I’m doing wrong.

Code Behind:

  protected Dictionary<Foo, List<Bar>> FooDictionary         {             get;              set;          }  protected void DataBindFooList(List<int> SelectedFoos)         {             System.Web.UI.WebControls.ListView lv = lvFooList;  try             {                 Dictionary<Foo, List<Bar>> fooDictionary =                     new Dictionary<Foo, List<Bar>>();                  foreach (int Foo in SelectedFoos)                 {                  // Build List of Foos to add as Dictionary.Keys                  fooDictionary.Add(fooScalar, Bar)                                      }                 FooDictionary = fooDictionary;                 lv.DataSource = FooDictionary;                 lv.DataBind();                 ddlListOfBars.DataSource = FooDictionary;                 ddlListOfBars.DataValueField = 'ItemValue';                 ddlListOfBars.DataTextField = 'ItemName';                 ddlListOfBars.DataBind();             }             catch (Exception ex)             {                 SetMessage(divFooMsg, 'Unable to retrieve Foos: ' +                  ex.Message, true, true);             } 

The ListView Code:

<asp:ListView ID='lvFooList' runat='server'>    <LayoutTemplate>       <asp:PlaceHolder runat='server' ID='itemPlaceholder'></asp:PlaceHolder>    </LayoutTemplate>       <ItemSeparatorTemplate>          <hr />       </ItemSeparatorTemplate>    <ItemTemplate>       <%#Eval('Name') %>       <asp:DropDownList ID='ddlListOfBars' runat='server'></asp:DropDownList>     </ItemTemplate>    </asp:ListView> 

The Question(s):

  1. Is it possible to use a Dictionary in this way?
  2. Any pointers on where I’m going wrong? (Resources, hints, etc. would help)
  3. Is there a better way to do this?
  4. If this question is clear as mud, please comment so I can figure out how to improve it.
  • 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. 2026-05-11T07:48:58+00:00Added an answer on May 11, 2026 at 7:48 am

    Your problem arises because it doesn’t make sense to databind ddlListOfBars in DataBindFooList(), because there isn’t just one DropDownList to databind. When you call lv.DataBind(), the ListView creates a copy of your ItemTemplate for each Foo, each containing a ddlListOfBars. You need to tell it to bind each DropDownList to the right List<Bar> as it goes. You can do this by setting the datasource of ddlListOfBars with data binding expressions rather than in the code behind:

    <ItemTemplate>   <%#Eval('Key.Name') %>   <asp:DropDownList         ID='ddlListOfBars'         runat='server'         DataSource='<%#Eval('Value')%>'         DataValueField='ItemValue'         DataTextField='ItemName' /> </ItemTemplate> 

    Note that you also need to use ‘Key.Name’ to get to the name property of your Foo. The individual dictionary items that you’re binding to are KeyValuePair<Foo,List<Bar>>, which has a Key property and a Value property to access the Foo and List<Bar> respectively.

    Edit
    If you’ve got a lot going on in your ItemTemplate then it can be useful to move the contents out into a user control. The user control can have a strongly-typed property to access the DataItem, and has strongly-typed access to its child controls. This gets you the flexibility of the ItemDataBound event without all the the casting and FindControl() noise. I doubt I’d bother in this case, but it would go something like

    <asp:ListView ID='lvFooList' runat='server'> <LayoutTemplate>   <asp:PlaceHolder runat='server' ID='itemPlaceholder'></asp:PlaceHolder> </LayoutTemplate>   <ItemSeparatorTemplate>      <hr />   </ItemSeparatorTemplate> <ItemTemplate>   <uc:ListViewContents DataItem='<%# Container.DataItem %>' /> </ItemTemplate> 

    ListViewContents.ascx…

    <asp:Label ID='lbName' runat='server'/> <asp:DropDownList ID='ddlListOfBars' runat='server'></asp:DropDownList> 

    ListViewContents.ascx.cs…

    public KeyValuePair<Foo,List<Bar>> DataItem {     get; set; }  protected override void OnDataBinding(EventArgs e) {     base.OnDataBinding(e);      lbName.Text = DataItem.Key.Name;      ddlListOfBars.DataTextField = 'ItemName';     ddlListOfBars.DataValueField = 'ItemValue';     ddlListOfBars.DataSource = DataItem.Value;     ddlListOfBars.DataBind();    } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 140k
  • Answers 140k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the IsNull built-in function. SELECT PERSONID, CARDEVENTDATE,… May 12, 2026 at 7:49 am
  • Editorial Team
    Editorial Team added an answer I'm using CDO (Connected Data Objects) in conjunction with EMF… May 12, 2026 at 7:49 am
  • Editorial Team
    Editorial Team added an answer I could do this using PropertyFilter. I defined the following… May 12, 2026 at 7:49 am

Related Questions

Preamble I'm asking this question because even though I've read through a lot of
Preamble: I am using ASP.NET 3.5 and AJAXControlToolkit. I am trying to use the
A preamble: I've been using PHP for over 8 year every now and then,
The preamble: I have designed a strongly interfaced and fully mockable data layer class
Preamble So, this question has already been answered, but as it was my first

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.