Count number of checkbox selected

Asked

Viewed 1,374 times

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 that way, but always takes all the checkboxes, I want only the selected.

2 answers

0


@Warlock, I really don’t understand why you need to count the Checkboxes every Callback.

If you need to do something that takes into account the selected Checkboxes, the logical thing would be that the event was triggered by an external Button to the Repeater.

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

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++;
        }
    }
}
  • I’ve done it that way, but then it shows everyone. I just want an external boot (delete in case) to delete more than one.So I was just preparing before. store these values, but then it didn’t work.

  • No need to store in a Preview way, the external Button can scroll through the Repeater Items and read the Repeater Items data

  • So how would you look at another event? the deletar event calls a Deletardiretorio() method, within this method it takes the selected cb, but only takes 1 the current one that comes in the Viewstate, then as it would be, because if I call the Rtitem in another method, from null

-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 I made no loop.

If you want to re-check the checkboxes whenever someone checks / deselect you need to pick item and find the checkbox inside it and check that checkbox.

I don’t think this is a good solution, you could work with a variable that maintains status and every click add or subtract based on the value of checked.

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 the more items enter the Reset.

I will put an example (I do not know if it will run, if it does not run put the error that I fix):

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
}

Worth saying in the Page_Load initialize the Session["cont"] = 0 (if not postback) to prevent strange behavior from occurring while browsing the site.

  • i do not know what the solution, post the solution as answer pf.

  • Yes sir! Posted for you convenience.

  • so I check if it’s been checked?

  • is just playing Session inside the for? I don’t understand

  • It’s easier if you say what you want to do, I did a snipet based on your example.

  • I want to show the selected total

  • So man, the value must be inside the cont, try the code.

  • I said show, it only counts 1, when selecting another item does not increase.

  • chat now.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.