检查Asp.Net中检查的外部Repeater内部Repeater内部的CheckBox

时间:2017-08-27 03:53:13

标签: c# asp.net checkbox repeater nested-repeater

有两个CheckBox,一个在外部Repeater中,另一个在内部Repeater内。

我想在检查外部Repeater的CheckBox时自动检查内部Repeater的CheckBox。

HTML标记:

<asp:Repeater ID="repOuter" runat="server">
    <ItemTemplate>
        <asp:CheckBox ID="chkOuter" runat="server" 
             OnCheckedChanged="chkOuter_CheckedChanged" />

        <asp:Repeater ID="repInner" runat="server">
            <ItemTemplate>
                <asp:CheckBox ID="chkInner" runat="server" />
            </ItemTemplate>
        </asp:Repeater>

    </ItemTemplate>
</asp:Repeater>

代码隐藏

protected void chkOuter_CheckedChanged(object sender, EventArgs e)
{
    // can't have access to inner CheckBox placed inside Inner Repeater
    CheckBox innerCheckBox; // how?

    innerCheckBox.Checked = true; // want to check it
}

1 个答案:

答案 0 :(得分:1)

你可以尝试这个(虽然未经测试)。

CheckBox chkOuter = (sender as CheckBox);
RepeaterItem item = (RepeaterItem)chkOuter.NamingContainer;   
Repeater rp = item.FindControl("repInner") as Repeater;
foreach (RepeaterItem item in rp.Items)
{    
    (item.FindControl("chkInner") as CheckBox).Checked = chkOuter.Checked;
}