我正在尝试将嵌套的 ListView 工作好几天,但我无法弄清楚为什么它只显示第一个选定的数据。这是一些代码:aspx:
<asp:ListView ID="ListForumCategories" runat="server">
<ItemTemplate>
<div class="accordionButton">
<table><%--class="Categories"--%>
<tr>
<td class="CatName">
<%# Eval("CatName") %>
</td>
<td class="SubjectNumber">
0
</td>
<td class="AllComment">
0
</td>
<td class="CreationDate">
<%# Eval("WhenCreated") %>
</td>
<td class="LastModification">
<%# Eval("WhenCreated") %>
</td>
</tr>
</table>
</div>
<asp:ListView ID="ListSubjectsToCategories" DataSource='<%# Eval("RelatedSubjects") %>' runat="server">
<ItemTemplate>
<div class="accordionContent"><%# Eval("SubjID") %> <%# Eval("SubjectName") %> <%# Eval("WhenCreated") %></div>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
查询
public static List<ForumCategoriesModel> ListForumCategories()
{
try
{
return BW_Model.ForumCategories.Select(x => new ForumCategoriesModel
{
CatID = x.CatID,
CatName = x.CatName,
WhenCreated = x.WhenCreated
}).ToList();
}
catch
{
throw new Exceptions.SomethingWrongException();
}
}
public static List<ForumSubjectsModel> ListForumSubjectByCategoryID(int catid)
{
try
{
return BW_Model.ForumSubjects.Select(x => new ForumSubjectsModel
{
SubjID = x.SubjID,
WhoCreated = x.WhoCreated,
WhichCategory = x.WhichCategory,
SubjectName = x.SubjectName,
SubjectText = x.SubjectText,
WhenCreated = x.WhenCreated
}).Where(x => x.WhichCategory == catid).ToList();
}
catch
{
throw new Exceptions.SomethingWrongException();
}
}
班级
public class ForumCategoriesModel
{
public Int32 CatID { get; set; }
public String CatName { get; set; }
public DateTime WhenCreated { get; set; }
public List<ForumSubjectsModel> RelatedSubjects { get { return bw_forum_sqlchannel.ListForumSubjectByCategoryID(CatID); } }
}
public class ForumSubjectsModel
{
public Int32 SubjID { get; set; }
public Int32 WhoCreated { get; set; }
public Int32 WhichCategory { get; set; }
public String SubjectName { get; set; }
public String SubjectText { get; set; }
public DateTime WhenCreated { get; set; }
}
我应该得到一个 List<> 回来,有很多元素,但它只显示每个类别的第一个。
现在试图解决这个问题 2 天......如果你能帮助我,那真的很感激。提前致谢。
啊,忘了后面的代码:):
categories = bw_forum_sqlchannel.ListForumCategories();
ListForumCategories.DataSource = categories;
ListForumCategories.DataBind();
解决方案:
<asp:ListView ID="ListSubjectsToCategories" DataSource='<%# Eval("RelatedSubjects") %>' runat="server">
<ItemTemplate>
<div class="accordionContent"><%# Eval("SubjID") %> <%# Eval("SubjectName") %> <%# Eval("WhenCreated") %></div>
</ItemTemplate>
</asp:ListView>
变成:
<div class="accordionContent">
<asp:ListView ID="ListSubjectsToCategories" DataSource='<%# Eval("RelatedSubjects") %>' runat="server">
<ItemTemplate>
<%# Eval("SubjID") %> <%# Eval("SubjectName") %> <%# Eval("WhenCreated") %>
</ItemTemplate>
</asp:ListView>
</div>