繁体   English   中英

在另一个按钮单击事件中的转发器中查找CheckBox

[英]Find CheckBox Inside a repeater in another button click event

我有两个嵌套的中继器和一个复选框,像这样

<asp:Repeater ID="rptInterestCategory" runat="server" OnItemDataBound="rptInterestCategory_ItemDataBound">
        <ItemTemplate>
            <asp:Repeater ID="rptInterests" runat="server" OnItemDataBound="rptInterests_ItemDataBound">
                <ItemTemplate>
                    <asp:CheckBox ID="cbInterest" runat="server" OnCheckedChanged="cbInterest_CheckedChanged" Data-Id='<%# DataBinder.Eval(Container.DataItem, "id") %>' Text='<%# DataBinder.Eval(Container.DataItem, "name") %>' />

                </ItemTemplate>
            </asp:Repeater>
            <hr/>
        </ItemTemplate>
    </asp:Repeater>

现在在另一个按钮单击事件上,我想查找所有选中的复选框( cbInterest ),需要获取其中的值。 这样做的正确方法是什么?

遍历它们的方法是首先获取对嵌套Repeater的引用:

Repeater rptInterests = (Repeater)rptInterestCategory.FindControl("rptInterests");

然后,您可以遍历数据项,找到复选框,并获取CheckBox的值:

foreach (RepeaterItem item in rptInterests.Items)
{
    CheckBox cbInterest = (CheckBox)item.FindControl("cbInterest");
    bool isChecked = cbInterest.Checked;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM