문제

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

도움이 되었습니까?

해결책

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!

다른 팁

<%= (Repeater)ChildRepeater.NamingContainer =>

or use it directly in your code-behind without the <%= and =>

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top