views:

794

answers:

3

Background

In our last adventure, I was attempting to properly show a Dictionary<Foo, Bar<T>>. Thanks to the helpful answers in that post, I realize I had been going about it in the wrong way.

Problem

In today's adventure, I've been trying to get two specific properties from the List<T> in the last question to display. Each List has specific properties, shown below:

---------------------
|     Bar Class     |
---------------------
| Id        int     |
| Foo.Id    int     |
| Name      string  |
| Type      string  |
---------------------

For display, I'd like to display the Bar.Name + Bar.Type, concatenated together. Obviously I have to retain the complete object, since the Id will be passed back to be sent to the database.

At first, I tried <%#Eval("Value.Type" + "Value.Name")%>, clearly that did not work. My guess now is that I need to write a foreach loop to iterate through the Dictionary Object, and retrieve the Dictionary.Value Objects intact, and build a dropdown list out of that. But then, the problem becomes tying that back to the associated profile, and sending it back to the database wholesale.

Here currently is how the page looks:

<tr><td align="center"> <%#Eval("Key.Name") %></td>
    <td align="center"><asp:DropDownList ID="ddlListOfBars" runat="server" 
DataSource='<%#Eval("Value")%>' DataValueField="Id" DataTextField="Type" />
</td>

I've been monkeying around with different ways of changing the DataTextField but haven't yet figured out a combination that works.

Questions

  1. Am I overthinking this?
  2. What am I missing?
  3. Is there a resource for how you would pull specific properties out of the List<T> to use those values as the display Values for the DropDownList, and if so, is there a good resource on that topic?
  4. Is this post as clear as mud?
+1  A: 

Maybe I'm missing something (didn't look at the earlier question), but why wouldn't you just do this?

<%# Eval("Name") + Eval("Type") %>
Bryan
I've done a lot of WinForms, not so many webforms.
George Stocker
@Gortok - I feel your pain, web forms are HARD coming from WinForms!
Gavin Miller
Thanks for the answer. It didn't help (see my edits to my question, as well as my post below), but thanks for trying. Specifically, it's the 'DataTextField' that was having issues. If you've got a way to concatenate it there, let me know, otherwise I'm just going to use my solution below.
George Stocker
+2  A: 

As it turns out, the problem is easily solvable if the two columns from the table are concatenated and into one column, like so:

SELECT Column1Name + ' - ' + Column2Name AS Column3Name FROM Table1 WHERE...

This turns this:

Column1Name          Column2Name
-----------          ----------- 
Stuff1               Stuff5
Stuff2               Stuff6
Stuff3               Stuff7
Stuff4               Stuff8

Into this:

Column3Name
---------------
Stuff1 - Stuff5
Stuff2 - Stuff6
Stuff3 - Stuff7
Stuff4 - Stuff8

When all else fails, think a little lower level.

George Stocker
This is a very reasonable solution. In this case I would create a view so the concatenation was more or less seemless from the app's perspective.
Bryan
A: 

I had to convert the object to a string before concatenating the string. This worked for me.

Text='<%# Eval("LastName").ToString() + ', ' + Eval("FirstName").ToString() %>'

John J