我花了几个小时来研究这个问题。我有以下课程
public class Tasks
{
public string Id { get; set; }
public string Name { get; set; }
public Dictionary<int,string> SubTasks { get; set; }
}
我的问题是当我List<Tasks>
如何将其绑定到网络表单时。我将能够绑定Id和Name。但是无法绑定SubTasks列表
网络表单
<table class="table1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Sub</th>
</tr>
<asp:ListView ID="Listview1" runat="server">
<LayoutTemplate>
<ol>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</ol>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("Id")%></td>
<td><%# Eval("Name")%></td>
<%--<td>????</td>--%>
</tr>
</ItemTemplate>
</asp:ListView>
</table>
我需要这种输出
------------------------------
id |Name |sub |
-----------------------------
1 | abc | 1a aaa |
| | 1f sss |
| | 1v ggg |
-----------------------------
2 | ewq | 2a ccc |
-----------------------------
3 | rty | 3a bbb |
-----------------------------
答案 0 :(得分:0)
这是一种不同的方法,但有效
在你的.aspx中添加一个Literal控件
即
<body>
<form id="form1" runat="server">
<div>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</div>
</form>
</body>
并在你.cs文件
private void Fill()
{
var list = new List<Tasks>();
// this is just to populate some sample data
for(var i=1;i<5;i++)
{
var obj = new Tasks();
obj.Id = "Id"+i.ToString();
obj.Name = "Name " + i.ToString();
var d = new Dictionary<int, string>();
for(var j=1;j<3;j++)
{
d.Add(j,"sub"+j.ToString());
}
obj.SubTasks=d;
list.Add(obj);
}
Literal1.Text = "<table class='table1'><tr><th>ID</th><th>Name</th><th>Sub</th></tr>";
foreach (var item in list)
{
var str = "<tr>";
str +="<td>"+ item.Id.ToString()+"</td>";
str += "<td>" + item.Name+ "</td>";
if(item.SubTasks!=null)
{
str += "<td><table>";
foreach(var sub in item.SubTasks)
{
str +="<tr>";
str += "<td>" + sub.Key.ToString() + "</td>";
str += "<td>" + sub.Value.ToString() + "</td>";
str += "</tr>";
}
str += "</td></table>";
}
str += "</tr>";
Literal1.Text += str;
}
Literal1.Text +="</table>";
}
现在您只需要将某些样式应用于表格以使显示正确
希望这会有所帮助!!