[RESOLVED] multiple nested repeaters
Hi, I have created a nested repeater using dataset relations before, but am having real trouble adign a third level
Code:
<asp:Repeater ID="rptModules" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:yellow;"><td><%#Eval("modTitle")%></td></tr>
<asp:Repeater ID="rptSlides" runat="server">
<ItemTemplate>
<tr style="background-color:Cornsilk;"><td><%#Eval("slideid")%></td></tr>
<asp:Repeater ID="rptUserAnswers" runat="server">
<ItemTemplate>
<tr style="background-color:SpringGreen;"><td> <%#Eval("answertext")%> </td></tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
In the code behind
Code:
Public Sub LoadData(ByVal reload As Boolean)
If reload = True Then
Dim myConnection As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("DatabaseURL"))
Dim myCommand As New SqlCommand("SelectBestAnswer", myConnection)
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.Add("@userID", SqlDbType.UniqueIdentifier)
myCommand.Parameters("@userID").Value = UserID()
Dim ad As New SqlDataAdapter(myCommand)
ds = New DataSet
ad.Fill(ds)
ds.Relations.Add(New DataRelation("modSlide", ds.Tables(0).Columns("moduleID"), ds.Tables(1).Columns("moduleID")))
ds.Relations.Add(New DataRelation("slideAnswer", ds.Tables(1).Columns("SlideID"), ds.Tables(2).Columns("SlideID")))
If Not ds Is Nothing Then
rptModules.DataSource = ds.Tables(0)
rptModules.DataBind()
End If
myConnection.Close()
End If
End Sub
Protected Sub rptModules_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptModules.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim dr As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim innerRep As Repeater = TryCast(e.Item.FindControl("rptSlides"), Repeater)
Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
If Not IsNothing(innerRep) Then
innerRep.DataSource = drv.CreateChildView("modSlide")
innerRep.DataBind()
End If
End If
End Sub
Do I have to cast the third repeater and then databind it the same as the second one?
Can anyone help as I ahve been struggling with this for a couple of hours
Re: multiple nested repeaters
Hello,
Is it really necessary to go to that level of repeating? I am not saying that it is impossible, but rather a hint that the layout that you are using is not quite right?!?
Gary
Re: multiple nested repeaters
Hi Gep, not strictly necessary and I have already been considering other options, but here is scenario.
This is to show the best answers given when taking a quiz. So the 3 levels of the repeater are
Quiz taken 1
-> Question1
->-> Best answer
->-> Best answer
-> Question2
->-> Best answer
->-> Best answer
Quiz taken 2
-> Question1
->-> Best answer
->-> Best answer
-> Question2
->-> Best answer
->-> Best answer
etc.
I have seen examples where this is achieved by adding the datasource for the third level repeater in the source file.
Code:
<asp:Repeater ID="rpt_gchild" runat="server" DataSource='<%# ((DataRow)Container.DataItem).GetChildRows("slideAnswer") %>'>
I couldnt get this working, possibly because this is c# and I haven't converted it correctly.
Re: multiple nested repeaters
Got it working Gary, managed to find a solution on here after about 50 google searches
http://www.vbforums.com/showthread.php?t=528713
All I had to do was chnage this line
<asp:Repeater ID="rptUserAnswers" runat="server" DataSource='<%# DirectCast(Container.Dataitem, Data.DataRowView).CreateChildView("slideAnswer") %>'>
And my databound for top level repeater looks like
Code:
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim dr As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim innerRep As Repeater = TryCast(e.Item.FindControl("rptSlides"), Repeater)
Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
If Not IsNothing(innerRep) Then
innerRep.DataSource = drv.CreateChildView("modSlide")
innerRep.DataBind()
Dim innerRep2 As Repeater = TryCast(innerRep.FindControl("rptUserAnswers"), Repeater)
If Not IsNothing(innerRep2) Then
innerRep2.DataBind()
End If
End If
End If
Thanks for your time again mate
Re: [RESOLVED] multiple nested repeaters
I like the fact that the solution was found in VB Forums, goes to show that it is always a good idea to search the forums first :)
Glad you got it working!!
Gary