具有嵌套转发器的 ASP.NET Repeater 控件

标签 asp.net data-binding repeater

我有一个嵌套的转发器控件。 HTML 如下所示:

<ItemTemplate>
    <div class="boxLeft">
        <h4><%# DataBinder.Eval(Container.DataItem, "DisciplineName") %></h4><asp:Label runat="server" ID="lblDisciplineID" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "DisciplineID") %>'></asp:Label>
        <p><%# DataBinder.Eval(Container.DataItem, "DisciplineNarrative") %></p>
        <h5 class="articles"><%# DataBinder.Eval(Container.DataItem, "InstructorCount")%> instructors</h5>
        <ul>
            <asp:Repeater runat="server" ID="rptRegions">
                <ItemTemplate>
                    <li><a href='/Lessons/<%# DataBinder.Eval(Container.DataItem, "DisciplineName") %>/<%# DataBinder.Eval(Container.DataItem, "CityName") %>'><%# DataBinder.Eval(Container.DataItem, "CityName") %></a></li>
                </ItemTemplate>
            </asp:Repeater>
        </ul>
    </div>
</ItemTemplate>

如您所见,我想在子中继器中从父中继器访问父 DisciplineName 属性,以便构建 URL。我收到以下错误:

DataBinding: 'GolfLessonSearch.Model.CityEntity' does not contain a property with the name 'DisciplineName'.

这是因为它试图从子中继器获取“DisciplineName”,但我希望从父中继器获取它。我认为这些属性可能仍在范围内,但似乎不在范围内。有什么办法可以得到这个吗?

最佳答案

如果 DataItem 后面的对象与您尝试在转发器中表示的对象具有相同的父/子关系,则您始终可以完全限定属性名称,以便您的子转发器调用

DataBinder.Eval(Container.DataItem, "Parent.DisciplineName")

如果情况并非如此,我的直觉是为您的子对象创建一个 ViewModel 对象(即使它不是 mvc)来伪造这种关系...

编辑

请注意,当我说“Parent.DisciplineName”时,我的意思是用对象名称替换“Parent”...(我之所以符合条件,是因为“Parent”在 asp.净...

关于具有嵌套转发器的 ASP.NET Repeater 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8729989/

相关文章:

asp.net - 如何将 ASP.NET 中继器控件绑定(bind)到 IList<String>?

javascript - 具有 .m4v 格式的视频在我的网络中不在根目录中下载

c# - 使用 C#.NET 检索链接到 Windows Azure 网站的 SQL Azure 数据库的连接字符串

c# - 如何将 WPF TreeView 项的扩展事件绑定(bind)到 View 模型

c# - 中继器使用哪个事件来查找中继器内的控件

javascript - Jquery Repeater 解析 jquery 中的值

asp.net - 下拉列表 selectedindex 在页面刷新时不起作用

c# - 在 azure 上发布时列名无效

c# - Silverlight 3 和 IMultiValueConverter

c# - WPF 用户控件中的数据绑定(bind)