0

有两个 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
}
4

1 回答 1

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;
}
于 2017-08-27T04:45:46.867 回答