Asp.net嵌套中继器?

时间:2015-08-07 16:19:44

标签: asp.net vb.net repeater

我有评论部分,其中每篇评论都有一条或多条评论。需要使用嵌套转发器显示评论。

<asp:Repeater id="RepeaterReviews" runat="server" OnItemDataBound="ItemBound" >
                        <ItemTemplate>
                            <tr>
                                <td colspan="3">
                                    <b style="font-size:12pt;"><%#Eval("Username")%></b>
                                    <br />
                                    <b><%#Eval("Title")%></b>
                                    <br /><br />
                                </td>
                            </tr>   

                        <asp:Repeater id="RepeaterComment" runat="server"  >
                                  <ItemTemplate>
                                      <tr>
                                          <td colspan="3">
                                              <b style="font-size:12pt;"><%#Eval("Username")%></b>
                                              <br />
                                              <b><%#Eval("Comment")%></b>
                                              <br /><br />
                                          </td>
                                      </tr>   
                              </ItemTemplate>
                              </asp:Repeater>
    </ItemTemplate>
   </asp:Repeater>

背后的代码

Sub ItemBound(sender As Object, e As RepeaterItemEventArgs)
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
             Dim ReviewID As Integer = CType(Eval("ReviewID"), Integer) 'doesn't work
          Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
          Dim RepeaterComment As Repeater = CType(e.Item.FindControl("RepeaterComment"), Repeater)
end if
end sub

我所拥有的是评论和评论之间的一对多关系。 如何从第一个ReviewID数据源获取RepeaterReviews

1 个答案:

答案 0 :(得分:0)

您在ItemBound方法中使用的技术非常接近。您只需要使用DataRowView实例(drv)上的Item属性来获取值。

在这个例子中,我跳过了制作drv变量并完成了一行:

If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then

    Dim ReviewID As Integer = CInt(CType(e.Item.DataItem, DataRowView).Item("ReviewID"))

    'Do what you want with the ReviewID

End If

现在所有这些假设您在DataTable中有一个ReviewID字段绑定到Repeater。如果不这样做,这将无效。另外需要注意的是,如果您绑定自定义审阅类的通用列表,则需要进行一些调整,如下例所示:

Public Class Review
    Public Property ReviewID As Integer
    Public Property Username As String
    Public Property Title As String
End Class

Private Sub LoadData()

    Dim lstReviews As New List(Of Review)

    Dim objReview As New Review

    objReview.ReviewID = 1
    objReview.Title = "Test"
    objReview.Username = "User"

    lstReviews.Add(objReview)

    RepeaterReviews.DataSource = lstReviews
    RepeaterReviews.DataBind()  

End Sub

在这种情况下,你必须改变你的ItemBound代码,因为绑定到RepeaterItem的底层数据不再是DataRowView,而是一个Review Class实例。

If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then

    Dim ReviewID As Integer = CType(e.Item.DataItem, Review).ReviewID

    'Do what you want with the ReviewID

End If