0

I have a ListView with a couple of checkboxes. But if I want to know if the checkboxes are checked, it is always false (even if i checked it).

This is the code of my ListView

<asp:ListView ID="lvCompanies" runat="server" DataKeyNames="id" onitemdatabound="lvCompanies_ItemDataBound">
    <LayoutTemplate><ul><asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder></ul></LayoutTemplate>
    <ItemTemplate>                
        <li>
            <asp:CheckBox ID="cbCompany" CssClass="checkbox company-checkbox"  runat="server" />
            <%# Eval("Name") %>
        </li>
    </ItemTemplate>
</asp:ListView>  

And this is how I tried to get the checked Checkboxes

    public List<Company> getSelectedItems()
    {
        foreach (ListViewDataItem dataItem in lvCompanies.Items)
        {
            bool isChecked = ((CheckBox)dataItem.FindControl("cbCompany")).Checked;

        }
    }

Do you have any idea why my checkbox have always Checked = false?

Thanks a lot,

Vincent

1
  • 2
    inside which method/event handler are you calling the method getSelectedItems ? Commented Feb 17, 2012 at 13:46

1 Answer 1

4

Check that you have bounded lvCompanies in (!Page.IsPostBack)

Sign up to request clarification or add additional context in comments.

4 Comments

How is getSelectedItems() function called?
If you want to maintain the state of your checkboxes then follow this link evonet.com.au/maintaining-checkbox-state-in-a-listview
When you click a button (outside the listview) this function is called
@Vinzcent if this is correct answer then mark it by selecting the check mark.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.