串联在DropDownList中显示为两个属性 [英] Concatenating Two Properties for Display in a DropDownList
问题描述
背景
在我们最后的<一个href=\"http://stackoverflow.com/questions/583689/dictionaryt-of-listt-and-listviews-in-asp-net\">adventure,我试图正确地显示词典&LT;富,酒吧和LT; T&GT;&GT;
。由于在该职位的有用的答案,我意识到我已经在错误的道路已经持续了。
问题
在今天的冒险,我一直试图让从名单,下两个特定的属性; T&GT;
中的最后一个问题来显示。每个列表都有特定的属性,如下图所示:
---------------------
|酒吧类|
---------------------
| ID INT |
| Foo.Id INT |
|名称串|
|类型串|
---------------------
有关显示,我想显示 Bar.Name + Bar.Type
,串连在一起。很显然,我有保留完整的对象,因为该ID将传回给发送到数据库中。
起初,我试图&LT;%#的eval(Value.Type+Value.Name)%&GT;
,显然没有奏效。我的猜测是现在,我需要写一个foreach循环通过字典对象进行迭代,并检索 Dictionary.Value
对象不变,并建立一个下拉列表出来的。但随后,问题就变得绑了回相关的配置文件,并把它发回数据库批发。
下面是目前页面的外观:
&LT; TR&GT;&LT; TD ALIGN =中心&GT; &LT;%#的eval(Key.Name)%&GT;&LT; / TD&GT;
&LT; TD ALIGN =中心&GT;&LT; ASP:DropDownList的ID =ddlListOfBars=服务器
数据源='&LT;%#的eval(值)%GT;' DataValueField =IDDataTextField =输入/&GT;
&LT; / TD&GT;
我一直在改变了 DataTextField
,但还没有想出一个可行的组合不同的方式胡闹左右。
问题
- 我是不是该得太多?
- 我缺少什么?
- 是否有你会怎么拉特定属性出来的
列表℃的资源; T&GT;
来使用这些值作为显示值在DropDownList的
,如果是这样,是否有关于该主题的好资源? - 这是后那样清楚泥?
事实证明,问题是容易解决的,如果从表中的两列被连接进入一列,像这样:
SELECT Column1Name +' - '+ Column2Name AS Column3Name FROM表1 WHERE
...
这变成这样:
Column1Name Column2Name
----------- -----------
Stuff1 Stuff5
Stuff2 Stuff6
Stuff3 Stuff7
Stuff4 Stuff8
进入这个:
Column3Name
---------------
Stuff1 - Stuff5
Stuff2 - Stuff6
Stuff3 - Stuff7
Stuff4 - Stuff8
当一切都失败了,觉得低一点的水平。
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
- Am I overthinking this?
- What am I missing?
- 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 theDropDownList
, and if so, is there a good resource on that topic? - Is this post as clear as mud?
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.
这篇关于串联在DropDownList中显示为两个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!