如何获取asp中的所有复选框:在asp:Repeater中的CheckBoxList

时间:2016-02-23 09:25:42

标签: c# html asp.net

在页面加载时,将始终取消选中复选框,然后用户将选择相关选项,然后单击“保存”。复选框列表的大小可能不同。

我需要形成一个已选中复选框的列表并将它们传递给保存函数,我的代码如下:

HTML

<asp:Repeater runat="server" ID="RpTriggerEmails" OnItemDataBound="RpTriggerEmails_OnItemDataBound">
            <HeaderTemplate>
                <ul class="trigger-repeater">
            </HeaderTemplate>
            <ItemTemplate>
                <li class="trigger-repeater-item">
                    <h4><asp:Literal runat="server" ID="LtName"></asp:Literal></h4>
                    <asp:CheckBoxList runat="server" ID="CblAttendees"/>
                </li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>
            </FooterTemplate>
        </asp:Repeater>
    <asp:LinkButton  ID="PlainLinkButton1" runat="server" CausesValidation="true" OnClick="LbSave_OnClick"/>

C#

protected void RpTriggerEmails_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var email = e.Item.DataItem as TriggeredEmail;
            if (email != null)
            {
                CheckBoxList CblAttendees = e.Item.FindControl("CblAttendees") as CheckBoxList;
                if (CblAttendees != null)
                {
                    CblAttendees.DataSource = Controller.GetBooking().Attendee;
                    CblAttendees.DataBind();
                    CblAttendees.DataTextField = "Reference";
                    CblAttendees.DataValueField = "AttendeeId";
                }
            }
        }
    }
    protected void LbSave_OnClick(object sender, EventArgs e)
    {
        Controller.SendTriggers(chosenCheckBoxes);
    }

1 个答案:

答案 0 :(得分:0)

我不得不改变一些代码来获得我想要的结果,但这会得到正确的信息:

它不再使用CheckBoxList,因为这会引起枚举问题,将其更改为转发器被证明是最容易理解的(我是学生而且不是很有经验)。

HTML

<asp:Repeater runat="server" ID="RpTriggerEmails" Visible="False" OnItemDataBound="RpTriggerEmails_OnItemDataBound">
            <HeaderTemplate>
                <ul class="trigger-repeater">
            </HeaderTemplate>
            <ItemTemplate>
                <li class="trigger-repeater-item">
                    <asp:HiddenField runat="server" ID="HfTriggerEmailId"/>
                    <h4><asp:Literal runat="server" ID="LtTriggerName"></asp:Literal></h4>
                    <asp:Repeater runat="server" ID="RpAttendees" OnItemDataBound="RpAttendees_OnItemDataBound">
                        <HeaderTemplate>
                            <ul class="attendee-repeater">
                        </HeaderTemplate>
                        <ItemTemplate>
                            <li class="attendee-repeater-item">
                                <asp:HiddenField runat="server" ID="HfAttendeeId"/>                                
                                <asp:CheckBox runat="server" ID="CbAttendee"/>
                            </li>
                        </ItemTemplate>
                        <FooterTemplate>
                            </ul>
                        </FooterTemplate>
                    </asp:Repeater>
                </li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>
            </FooterTemplate>
        </asp:Repeater>

C#

protected void LbSave_OnClick(object sender, EventArgs e)
    {
        foreach (RepeaterItem riTrig in RpTriggerEmails.Items)
        {
            HiddenField hfTrig = riTrig.FindControl("HfTriggerEmailId") as HiddenField;
            if (hfTrig != null)
            {
                foreach (RepeaterItem riAtt in ((Repeater) riTrig.FindControl("RpAttendees")).Items)
                {
                    CheckBox cb = riAtt.FindControl("CbAttendee") as CheckBox;
                    HiddenField hf = riAtt.FindControl("HfAttendeeId") as HiddenField;
                    if (cb != null && cb.Checked &&
                        hf != null && !hf.Value.IsNullOrEmpty())
                        checkboxes.Add(new GenericPair<int, int>(int.Parse(hf.Value),
                            int.Parse(hfTrig.Value)));
                }
            }
        }

        Controller.SendTriggers(checkboxes);
    }