Click here to Skip to main content
15,888,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a database table of Place_Names which has a foreign key relationship to a Regions table. I want to display this data in a ascx page with each region as a header and its place name children as checkboxes below. Like in the graphic displayed here
checkboxes[^].
I'm thinking I need a repeater within a repeater?
Do I just need to set the data source of the outer Regions repeater server side and then the inner repeater to display the place name checkboxes can pick up the id of the region each time it repeats?

What I have tried:

I've looked at something like this, but I'm not sure how to set the datasource of the checkboxlist, particularly as it isn't immediately available as it is within a repeater

<asp:Repeater ID="rptPlaceNamesRegions" runat="server">
     <HeaderTemplate>
        <asp:Label runat="server" ID="lbl" Value='<%# Eval("Region") %>' />
    </HeaderTemplate>
    <ItemTemplate>
        <asp:CheckBoxList ID="cblPlaceNames" runat="server" DataTextField="Text"
            DataValueField="Value" RepeatDirection="Vertical" RepeatColumns="2">
            <asp:ListItem Text="All" Value="" Selected="true" />
        </asp:CheckBoxList>
    </ItemTemplate>
</asp:Repeater>
Posted
Updated 17-Apr-19 8:32am

Use the ItemDataBound event in the code behind. This is c# but hopefully you get the idea. Update the repeater to add the event

<asp:Repeater ID="rptPlaceNamesRegions" runat="server" OnItemDataBound="rptPlaceNamesRegions_ItemDataBound">
     <HeaderTemplate>
        <asp:Label runat="server" ID="lbl" Value='<%# Eval("Region") %>' />
    </HeaderTemplate>
    <ItemTemplate>
        <asp:CheckBoxList ID="cblPlaceNames" runat="server" DataTextField="Text"
            DataValueField="Value" RepeatDirection="Vertical" RepeatColumns="2">
        </asp:CheckBoxList>
    </ItemTemplate>
</asp:Repeater>


In the code-behind I am binding to a list of DataObject classes, but you obviously use your correct data

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        var data = new List<DataObject> { new DataObject { Region = "Region A" }, new DataObject { Region = "B" } };
        rptPlaceNamesRegions.DataSource = data;
        rptPlaceNamesRegions.DataBind();
    }
}

protected void rptPlaceNamesRegions_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    DataObject data = e.Item.DataItem as DataObject;

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
                
        CheckBoxList cblPlaceNames = (CheckBoxList) e.Item.FindControl("cblPlaceNames");

        // I'm hard-coding for the example but you will get the data based on the "data"
        // object
        cblPlaceNames.DataSource = new ListItem[] { new ListItem { Text = "One", Value = "1" },
            new ListItem { Text = "Two", Value = "2" },
            new ListItem { Text = "Three", Value = "3" },
            new ListItem { Text = "All", Value = "" }};

        cblPlaceNames.DataBind();
    }
}
 
Share this answer
 
Comments
stonypaul 16-Apr-19 7:29am    
I don't think the server side itemdatabound event for the outer repeater can see the checkboxlist because it is in the inner repeater?
F-ES Sitecore 16-Apr-19 7:34am    
This line of code gets a reference to it

CheckBoxList cblPlaceNames = (CheckBoxList) e.Item.FindControl("cblPlaceNames");

Your code doesn't have a nested repeater but if you want to get a repeated in a repeater the code is similar

Repeater rpt = (Repeater) e.Item.FindControl("rptYourRepeater");
stonypaul 16-Apr-19 8:48am    
OK this is what I have so far. sorry I didn't include the nested repeater first time.

<asp:repeater id="rptRegion" runat="server" onitemdatabound="rptRegion_ItemDataBound">
<headertemplate>
<asp:label id="lblRegion" runat="server" text="<%# Eval("OrgDescription") %>">

<itemtemplate>
<asp:repeater id="rptPlaceName" runat="server">
<itemtemplate>
<asp:checkboxlist id="cblPlaceName" runat="server" datatextfield="Text"
="" datavaluefield="Value" repeatdirection="Vertical" repeatcolumns="3">
<asp:listitem text="All" value="" selected="true">








Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
rptRegion.DataSource = GetOrgs()
rptRegion.DataBind()
End If
End Sub
Here's what you can do:

1. Bind your your child/nested Repeater at ItemDataBound event of your parent Repeater:

VB
Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
    If (e.Item.ItemType = ListItemType.Item) OrElse (e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim childRepeater As Repeater = TryCast(e.Item.FindControl("Repeater2"), Repeater)
        childRepeater.DataSource = GetDataForInnerRepeater() ' Set the data source here for your inner Repeater
        childRepeater.DataBind()
    End If
End Sub


2. Then on ItemDataBound event of your child/inner Repeater, you can access your CheckBoxList as already suggested:

VB
Protected Sub Repeater2_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
    If (e.Item.ItemType = ListItemType.Item) OrElse (e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim cbl As CheckBoxList = CType(e.Item.FindControl("cblPlaceNames"), CheckBoxList)
        cbl.DataTextField = "FieldName"
        cbl.DataValueField = "FieldName"
        cbl.DataSource = GetYourCheckBoxDataSource() ' Set the data source for your CheckBoxList here
        cbl.DataBind()
    End If
End Sub



Alternatively, if your inner Repeater is already bounded with data, you can try accessing the CheckBoxList at ItemDataBound event of your parent Repeater like this:

VB
Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
    If (e.Item.ItemType = ListItemType.Item) OrElse (e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim childRepeater As Repeater = CType(e.Item.FindControl("Repeater2"), Repeater)

        For Each item As RepeaterItem In childRepeater.Items

            If item.ItemType = ListItemType.AlternatingItem OrElse item.ItemType = ListItemType.Item Then
                Dim cbl As CheckBoxList = CType(item.FindControl("cblPlaceNames"), CheckBoxList)
                cbl.DataTextField = "FieldName"
                cbl.DataValueField = "FieldName"
                cbl.DataSource = GetYourCheckBoxDataSource()
                cbl.DataBind()
            End If
        Next
    End If
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900