嵌套ListView仅显示第一个选定的数据

时间:2013-07-04 06:55:44

标签: asp.net listview

我正在努力使工作成为嵌套的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&lt;&gt;返回,有很多元素,但它只显示每个类别的第一个元素。

现在试着解决这个问题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>

1 个答案:

答案 0 :(得分:0)

原因可能是:

  1. ListForumSubjectByCategoryID只返回一条记录
  2. 内心ListView
  3. 正在发生一些奇怪的事情
  4. 除了浏览器由于某种原因没有显示所有数据外,一切正常
  5. 根据您提供的代码(看起来很健全,并且我自己的快速测试表明它应该有效),问题可能是第三种选择。

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