Count number of checkboxes selected

0

I have a grid with repeater:

 <asp:Repeater ID="rtInlineBlock" runat="server">
                        <ItemTemplate>

                            <div class="block">
                                <asp:HiddenField ID="idDirectorio" runat="server" Value='<%# Eval("guid") %>' />
                                <asp:HiddenField ID="isFolder" runat="server" Value='<%# Eval("isFolder") %>' />

                                <asp:ImageButton ID="btSend" runat="server" ImageUrl='<%# Eval("imgPath") %>' OnClick="btSend_Click" />

                                <div class="bottom">
                                    <asp:CheckBox ID="check" runat="server"  OnCheckedChanged="check_CheckedChanged" AutoPostBack="true" />
                                    <asp:Label ID="lblNome" runat="server" Text='<%# Eval("xInfo") %>'  />
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>

And my selection event:

protected void check_CheckedChanged(object sender, EventArgs e)
    {
        var cbSend = sender as CheckBox;
        var rtItem = cbSend.Parent as RepeaterItem;

        int cont = 0;
        foreach (var item in rtInlineBlock.Items)
        {

            if (cbSend.Checked)
            {
                cont = cont + 1;
            }
        }

}

I tried this way, but it always picks up all the checkboxes, I just want the ones selected.

    
asked by anonymous 03.03.2015 / 21:30

2 answers

0

@WarLock, I really do not understand why you need to count CheckBoxes for each CallBack.

If you need to do some action that takes into account the selected CheckBoxes, the most logical would be for the event to be triggered by an external Button to Repeater.

But by returning the question, you can adapt my method to count CheckBoxes selected from the following:

protected void check_CheckedChanged(object sender, EventArgs e)
{
    var cont = 0;
    foreach (var rbItem in rtInlineBlock.Items)
    {
        var cbSend = rbItem.FindControl("check") as CheckBox;
        if (cbSend.Checked)
        {
            cont++;
        }
    }
}
    
04.03.2015 / 00:39
-1

You have a logic error there, cbSend is always the same object inside the loop, you are doing this:

 int cont = 0;
 if (cbSend.Checked)
 {
       cont = rtInlineBlock.Count;
 }

The difference is that I did without a loop.

If you want to recount checkboxes every time someone checks / unchecks you need to get item and find the checkbox inside it and check this checkbox.

I do not think this is a good solution, you could work with a variable that holds state and each click adds or subtracts based on the checked value.

It is an exchange, memory for processing, and in this case the cost of memory is too small to justify this repeated processing, which only worsens as more items enter the repeater.

I'm going to put an example (I do not know if it will run, if it does not run the error I get):

protected void check_CheckedChanged(object sender, EventArgs e)
{
        var cbSend = sender as CheckBox;

        // Acho que faltava um convert aqui... null é zero.
        int cont = Convert.ToInt32(Session["cont"]);

        Session["cont"] = cont = cbSend.Checked ? cont + 1 : cont -1;

        // Prossiga com o que você quer fazer
}

It is worth noting that Page_Load initializes Session["cont"] = 0 (if not postback) to prevent strange behavior from occurring during site navigation.

    
03.03.2015 / 21:50