简体   繁体   English

ASP.NET 父中继器项目的索引(在嵌套中继器中)

[英]ASP.NET Index of parent repeater item (in nested repeaters)

I have a nested repeaters, ie a parent repeater and a child repeater.我有一个嵌套中继器,即一个父中继器和一个子中继器。 The child repeater contains only one DropDownList control.子中继器仅包含一个 DropDownList 控件。 I have OnSelectedIndexChanged setup on DropDownList control.我在 DropDownList 控件上有 OnSelectedIndexChanged 设置。 I can get the index of the child repeater item when the drop down list's selection is changed.当下拉列表的选择发生变化时,我可以获得子中继器项目的索引。

My question is: How can I get the index of the parent repeater in which the drop down list's selection was changed.我的问题是:如何获取更改下拉列表选择的父中继器的索引。

Here is the sample code:这是示例代码:

<asp:Repeater runat="server" ID="ParentRepeater">
    <ItemTemplate>

        <asp:Repeater runat="server" ID="ChildRepeater">
            <ItemTemplate>
                <asp:DropDownList runat="server" ID="DropDownInChildRepeater" OnSelectedIndexChanged="DropDownInChildRepeater_OnSelectedIndexChanged" />
            </ItemTemplate>
        </asp:Repeater>

    </ItemTemplate>
</asp:Repeater>


protected void DropDownInChildRepeater_OnSelectedIndexChanged(object sender, EventArgs e)
{
    var dropDownInChildRepeater = (DropDownList)sender;
    var dropDownInChildRepeaterItem = (RepeaterItem)dropDownInChildRepeater.NamingContainer;

    var indexOfDropDownInChildRepaterItem = dropDownInChildRepeater.ItemIndex;

    //Question I need index of ParentRepeater in which sender resides
}

You were on the right track with the NaminContainer .您使用NaminContainer在了正确的轨道上。 You just need to go up the control tree.你只需要 go 向上控制树。 DropDownList > RepeaterItem > Repeater > RepeaterItem. DropDownList > RepeaterItem > Repeater > RepeaterItem。

//dropdown in child repeater
var dropDownInChildRepeater = (DropDownList)sender;

//repeater item in child repeater
var dropDownInChildRepeaterItem = (RepeaterItem)dropDownInChildRepeater.NamingContainer;

//child repeater
var childRepeater = (Repeater)dropDownInChildRepeaterItem.NamingContainer;

//repeateritem of parent repeater
var parentRepeaterItem = (RepeaterItem)childRepeater.NamingContainer;

//the item index of the parent repeateritem
var parentRepeaterItemIndex = parentRepeaterItem.ItemIndex;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM