0

I'm having some dramas with Nested Repeaters in ASP.NET using VB.

I have search through this and a few other forums but cannot work out my code problem. All of the other examples ive found have been using an XML Datasource, or using multiple tables within a dataset. My Dataset only contains 1 table with all the necessary fields.

I essentially want to create an Event Calendar... eg

-September
      Event 1
      Event 2
-October
      Event 3
-November
      Event 4
      Event 5
      Event 6

All of the info is stored in 1 Table in my Database.

My Code so Far...

aspx

<asp:repeater id="rptCalendar" runat="server">
    <ItemTemplate>
        <%#DataBinder.Eval(Container.DataItem, "Month")%>
        <asp:repeater id="NestedRepeater" runat="server">
            <ItemTemplate>
                <%#DataBinder.Eval(Container.DataItem, "EventName")%>
                <br>
            </ItemTemplate>
        </asp:repeater>
    </ItemTemplate>
</asp:repeater>

aspx.vb

Protected Sub rptCalendar_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCalendar.ItemDataBound
    Dim dv As DataRowView = e.Item.DataItem

    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim nestedrepeater As Repeater = e.Item.FindControl("NestedRepeater")
        Dim drv As DataRowView = DirectCast(e.Item.DataItem, DataRowView)
        nestedrepeater.DataSource = drv.CreateChildView("EventName")
        nestedrepeater.DataBind()
    End If
End Sub

My Page_Load event just loads the entire table into a dataset called dsEvents and binds it. The Database looks like this.

-Events
    EventID
    EventName
    EventDate
    Month
    Day

The error message im getting is "The relation is not parented to the table to which this DataView points."

I haven't worked with Nested Repeaters before so not too sure what im doing wrong.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rob
  • 99
  • 1
  • 3
  • 12
  • Doesn't `CreateChildView` operate on a parent/child relationship between *two* tables in the data? If you have only *one* table as you say, how do you expect this to work? What is the "child view" you're trying to create? – David Sep 24 '14 at 17:26
  • I did try it without the CreateChildView and it brought up another error DataBinding: 'System.Char' does not contain a property with the name 'EventName'. As I havent used NestedRepeaters before i assumed the CreateChildView was needed. – Rob Sep 24 '14 at 17:31
  • A) You tried *what*? B) *What* was the error? You certainly shouldn't be randomly trying things. What are you actually attempting to *do* here? According to your question you have *one* table, so logically how can you even populate nested repeaters with data? Aside from the implementation details of doing so, it doesn't make sense. – David Sep 24 '14 at 17:33
  • Apologies, i accidentally hit the enter key and it posted the comment. I have edited the comment to include answers to the questions. The reason why i am looking into Nested Repeaters is that I do not know how many events there are per month, I need to have the Month Displayed, but not if the following 3 events are in the same month. This seemed to be the most popular solution, plus I can see the use for it with other pages on the site. However if there is a better solution im all ears. – Rob Sep 24 '14 at 17:40
  • The problem you're facing isn't really a matter of nested repeaters, it's a matter of incorrectly modeled data. Since you only have one table you can't derive a related table from a column on that table. You basically need to create *two* tables which relate to one another based on how you're trying to view the data. Note that you don't necessarily need to use `DataTable`s for this, you can for example loop through your records and build custom objects which represent your data. Then bind the repeaters to those objects. – David Sep 24 '14 at 17:46
  • Thanks for the advice... I was under the impression that this sort of thing would be a bit more straight forward than it looks... Am i going about it the wrong way? Would there be a better solution to achieve what i'm after? – Rob Sep 26 '14 at 14:48
  • Well, essentially you need to transform your data into two relational elements. There are many ways to go about that. If you don't want to change the physical database structure then I would recommend creating custom objects in code which represent those elements. There could be a parent object which has a property that is a list of child objects. You'd loop through the data, building the parent and child objects (likely a nested loop of some sort) and then bind the repeater to the list of parent objects. The nested repeater would bind to each parent object's list of child objects. – David Sep 26 '14 at 14:50

0 Answers0