1

I have a nested repeaters, i.e. a parent repeater and a child repeater. The child repeater contains only one DropDownList control. I have OnSelectedIndexChanged setup on DropDownList control. 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
}

1 Answer 1

2

You were on the right track with the NaminContainer. You just need to go up the control tree. 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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.