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):
- Is it possible to use a Dictionary in this way?
- Any pointers on where I'm going wrong? (Resources, hints, etc. would help)
- Is there a better way to do this?
- If this question is clear as mud, please comment so I can figure out how to improve it.
See Question&Answers more detail:
os