Search code examples
asp.netrepeater

find parent repeater from child repeater's dropdownlist selectedindexchange


I want to find the Parent Repeater, which contains Child Repeater and Child Repeater contains dropdownlist. On SelectedIndexChange of the Drowndownlist I want to find out the Parent Repeater. After finding the parent repeater, I want to find the hiddenfield value inside Parent Repeater. i.e.

Parent Repeater Contains HiddenField and Child Repeater Child Repeater contains Dropdownlist on this dropdown selected index change event I want to find HiddenField value which is in Parent Repeater.

My Code:

        DropDownList myGeneralButton = (DropDownList)sender;
        Repeater item = (Repeater)myGeneralButton.Parent.Parent;

        for (int i = 0; i < item.Items.Count; ++i) 
        {
            HiddenField hdn=  item.Items[i].FindControl("Hdhotelname") as HiddenField;
            string h = hdn.Value;
        }

In this hidden field I am getting all the values, but I want a value of that particular index where I am selecting selecting the dropdown.

Thanks


Solution

  • You have to search through the DropDownList's NamingContainer. The flow should be like this:

    (DropDownList)sender
    --> NamingContainer(Child RepeaterItem)
    --> NamingContainer(Child Repeater)
    --> NamingContainer(Parent RepeaterItem)
    --> FindControl"Hdhotelname" (Hdhotelname)

    and your code should be like this:

    protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        var ddl = (DropDownList)sender;
        var rptChild = ddl.NamingContainer.NamingContainer;//Child Repeater
        if (rptChild != null)
        {
            var rptParentItem = rptChild.NamingContainer;//Parent RepeaterItem
            var hdnfld = rptParentItem.FindControl("Hdhotelname") as HiddenField;
            if (hdnfld != null)
            {
                //Do your tasks
            }
        }
    }
    

    Hope it helps!