0

我正在尝试将嵌套的 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>
4

1 回答 1

0

原因可能是:

  1. 唯一ListForumSubjectByCategoryID返回一条记录
  2. 内部发生了一些奇怪的事情ListView
  3. 一切都很好,除了浏览器由于某种原因没有显示所有数据

根据您提供的代码(看起来很理智,并且我自己的快速测试表明它应该可以工作),问题可能是第三种选择。

您可以通过查看呈现页面的源来检查内容是否存在?如果是这样,您需要调查您的 CSS 以找出浏览器仅显示第一行的原因。

于 2013-07-04T10:59:12.797 回答