单击按钮获取所有已选中复选框的值

时间:2019-02-18 01:06:42

标签: asp.net checkbox repeater

我在.aspx文件中创建了一个如下表:

enter image description here

这是执行此操作的代码:

<!-- code for generating the "add selected sessions" button -->
<table>
    <tr>
        <td><strong>Individual Sessions</strong></td>
        <td >
            <div class="addButton" style="text-align: center;">
                <asp:LinkButton ID="LinkButton2" runat="server" Text="Add Selected Sessions" OnClick="btnAddToCart_Click" />
            </div>
        </td>
    </tr>
</table>

<!-- add all the sessions for the user to select -->
<asp:Repeater ID="rptFeesSession" runat="server">
    <HeaderTemplate>
        <table >
    </HeaderTemplate>
    <ItemTemplate>
        <asp:HiddenField ID="hdnIsSession" runat="server" Value='<%#Eval("isSession")%>' />
        <tr runat="server" visible='<%# Eval("isSession")%>'>
            <td valign="top" colspan="2" style="position: relative;">
                <asp:HyperLink CssClass="siteColorFG popBtn" ID="hlFeeType" runat="server" Text='<%#Eval("title")%>' NavigateUrl="javascript:;"/>
            </td>
            <td valign="top">
                <div class="">
                    <asp:CheckBox ID="LinkButton3" CommandArgument='<%#Eval("id")%>'CssClass="checkB" OnClick="btnAddToCart_Click" runat="server" Text='<%#Eval("amount", "{0:C}")%>' />
                </div>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

在文件后面的代码中,我想捕获所有已选中的复选框及其各自的CommandArgument值。

protected void btnAddToCart_Click(object sender, EventArgs e)

{
    using (MyEntities db = new MyEntities())
    {
        //button was clicked. fetch all the check boxes from the rptFeesSession repeater into an int[]


    }
}

1 个答案:

答案 0 :(得分:0)

您的代码中有几个问题(包括概念/逻辑)

  1. Repeater中的项目事件应解决与项目相关的事情。
  2. Click事件处理程序无法访问CommandArgument属性。请改用Command
  3. Checkbox控件不支持onclick事件。
  4. Checkbox事件仅在有AutoPostback="true"时才能立即运行。

如果您想在更改任何复选框时刷新所有转发器数据,则可以执行以下操作。

<asp:ScriptManager runat="server" ID="scriptMgr" /><%-- Strongly recommended --%>
<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:Repeater ID="rptFeesSession" runat="server">
            <HeaderTemplate>
                <table>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:HiddenField ID="hdnIsSession" runat="server" Value='<%#Eval("isSession")%>' />
                <tr runat="server" visible='<%# Eval("isSession")%>'>
                    <td colspan="2" style="position: relative;">
                        <asp:HyperLink CssClass="siteColorFG popBtn" ID="hlFeeType" runat="server" Text='<%#Eval("title")%>' NavigateUrl="javascript:;" />
                    </td>
                    <td>
                        <div class="">
                            <asp:HiddenField runat="server" ID="hidID" Value='<%#Eval("id") %>' />
                            <asp:CheckBox ID="LinkButton3"
                                AutoPostBack="true" CssClass="checkB"
                                OnCheckedChanged="LinkButton3_CheckedChanged" runat="server"
                                Text='<%#Eval("amount", "{0:C}")%>' />
                        </div>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </ContentTemplate>
</asp:UpdatePanel>


//.cs
protected void LinkButton3_CheckedChanged(object sender, EventArgs e)
{
    decimal total = 0;
    using (MyEntities db = new MyEntities())
    {
        foreach (RepeaterItem item in rptFeesSession.Items)
        {
            var chk = item.FindControl("LinkButton3") as CheckBox;
            if(chk!=null && chk.Checked){
                string id = (item.FindControl("hidID") as HiddenField).Value;
                total += decimal.Parse(chk.Text);
                //do stuff
            }
        }
    }
}