【问题标题】:Find CheckBox Inside a repeater in another button click event在另一个按钮单击事件中的中继器内查找 CheckBox
【发布时间】:2014-03-14 21:18:58
【问题描述】:

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

<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)都被选中并需要获取其中的值。这样做的正确方法是什么?

【问题讨论】:

    标签: c# asp.net repeater


    【解决方案1】:

    循环遍历它们的方法是首先获取对嵌套中继器的引用:

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

    然后就可以循环遍历数据项,找到checkbox,得到CheckBox的值:

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

    【讨论】:

    • 错误 11 无法将类型“System.Web.UI.Control”隐式转换为“System.Web.UI.WebControls.Repeater”。存在显式转换(您是否缺少演员表?)
    • @Athul 对不起,忘记投那些 CheckBox 和 Repeater 控件。我已经更新了代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 2014-06-28
    • 2021-12-21
    相关资源
    最近更新 更多