Concatenating Two Properties for Display in a DropDownList

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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

<%# Eval("Name") + Eval("Type") %>

Method 3

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

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x