转发器内的转发器 [英] A repeater within a repeater

查看:101
本文介绍了转发器内的转发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Place_Names的数据库表,它与Regions表有一个外键关系。我想在ascx页面中显示这些数据,每个区域都作为标题,地址名称为子项,如下面的复选框。如此处显示的图形

复选框 [ ^ ]。

我想我在转发器中需要一个转发器?

我只需要设置外部区域转发器服务器端的数据源,然后内部转发器显示地址名称复选框可以在每次重复时获取该区域的ID吗?



我尝试了什么:



我看过这样的东西,但我不知道怎么设置复选框列表的数据源,特别是因为它不是立即可用的,因为它在转发器中



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>

推荐答案

在后面的代码中使用ItemDataBound事件。这是c#,但希望你明白了。更新转发器以添加事件



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>





在代码隐藏中,我绑定到DataObject类列表,但是你显然使用了正确的数据





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();
    }
}


这是你能做的:



1.绑定您的孩子/嵌套转发器 ItemDataBound 您父母的事件转发器



Here's what you can do:

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

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.然后在 ItemDataBound 事件您的孩子/内部转发器,您可以按照建议访问 CheckBoxList





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

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







或者,如果您的内部 Repeater 已经与数据绑定,您可以尝试访问 CheckBoxList at ItemDataBound 您父母的事件 Repeater 喜欢这个:






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:

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


这篇关于转发器内的转发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆