Vinzcent
Vinzcent

Reputation: 1448

ASP.NET CheckBox.Checked is always false in ListView

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

Upvotes: 0

Views: 1600

Answers (1)

Ravi Vanapalli
Ravi Vanapalli

Reputation: 9942

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

Upvotes: 4

Related Questions