从子中继器的下拉列表中查找父中继器 selectedindexchange

rah*_*wal 3 asp.net repeater

我想找到父中继器,其中包含子中继器,子中继器包含下拉列表。在下拉列表的 SelectedIndexChange 上,我想找出父中继器。找到父中继器后,我想找到父中继器内的隐藏字段值。IE

父中继器包含 HiddenField 和子中继器 子中继器包含此下拉选定索引更改事件上的下拉列表 我想找到父中继器中的 HiddenField 值。

我的代码:

        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;
        }
Run Code Online (Sandbox Code Playgroud)

在这个隐藏字段中,我获得了所有值,但我想要一个我选择下拉列表的特定索引的值。

谢谢

afz*_*ulh 5

你必须通过DropDownList's进行搜索NamingContainer。流程应该是这样的:

(DropDownList)发送者
--> NamingContainer(子RepeaterItem)
--> NamingContainer(Child Repeater)
--> NamingContainer(Parent RepeaterItem)
--> FindControl"Hdhotelname" (Hdhotelname)

你的代码应该是这样的:

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!