【问题标题】:Nested Repeater Problems. ASP.Net C#嵌套中继器问题。 ASP.Net C#
【发布时间】:2012-02-18 03:17:18
【问题描述】:

努力让嵌套中继器工作,尽管我觉得我很接近!

我正在尝试创建两个嵌套转发器,每个都绑定到我创建的类列表。

我目前收到此错误消息:

DataBinding: 'TR_BLL.Forum' does not allow indexed access.

这是页面的代码:

<!-- Forum Group Repeater -->
<asp:Repeater ID="rptForumGroups" runat="server" OnItemDataBound="rptForumGroups_ItemDataBound">
    <ItemTemplate>
        <div class="content">
            <div class="content-header">
                <h3><%# DataBinder.Eval(Container.DataItem, "strName")%></h3>
            </div>

            <!-- Forum Repeater -->
            <asp:Repeater ID="rptForums" runat=server>
                <ItemTemplate>
                    <%# DataBinder.Eval(Container.DataItem, "[\"strTitle\"]")%>
                </ItemTemplate>
            </asp:Repeater>
            <!-- End Forum Repeater -->

        </div>
    </ItemTemplate>    
</asp:Repeater>
<!-- End Forum Group Repeater -->

这是背后的代码:

        protected void Page_Load(object sender, EventArgs e)
    {
        // Bind Forum Groups
        TR_BLL.ForumGroups ForumGroups = new TR_BLL.ForumGroups();
        List<TR_BLL.ForumGroup> listForumGroups = new List<TR_BLL.ForumGroup>();
        listForumGroups = ForumGroups.GetAllForumGroups();
        rptForumGroups.DataSource = listForumGroups;
        rptForumGroups.DataBind();
    }

    protected void rptForumGroups_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        // Bind Forums
        TR_BLL.Forums Forums = new TR_BLL.Forums();
        List<TR_BLL.Forum> listForums = new List<TR_BLL.Forum>();
        listForums = Forums.GetAllForums();

        Repeater rptForums = (Repeater)e.Item.FindControl("rptForums");
        rptForums.DataSource = listForums;
        rptForums.DataBind();
    }

顶级中继器在未嵌套时与嵌套中继器一样工作正常。

【问题讨论】:

  • 为什么嵌套中继器中的 Eval 语句使用"[\"strTitle\"]" 而不是"strTitle"。问题似乎与 TR_BLL.Forum 课程有关,您可以发布吗?
  • 噢!我使用了 "[\"strTitle\"]" 因为我的初始解决方案使用了两个数据表。它适用于“strTitle”如果您做出回应,我会将其标记为答案。谢谢!

标签: c# asp.net


【解决方案1】:

在嵌套中继器内:

<%# DataBinder.Eval(Container.DataItem, "[\"strTitle\"]")%>

代码应该很可能是

<%# DataBinder.Eval(Container.DataItem, "strTitle")%>

如果没有更多关于 TR_BLL.Forum 类的知识,这是最可能的原因。

【讨论】:

  • 就我而言,我使用的是 ["Date"],需要删除括号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-29
  • 2015-06-01
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多