Akshay
Akshay

Reputation: 1706

Selecting items of a Listview using checkboxes in ASP .NET

I am trying to use checkboxes to select the items in a Listview. I have added a checkbox control in the , and they are displayed properly.

The problem is that Checked property never changes when I click on them. Why does this happen? And is there a workaround?

Here is the code:

<asp:ListView ID="ListView1" runat="server" 
    onitemcommand="ListView1_ItemCommand" 
    onitemdatabound="ListView1_ItemDataBound">
    <LayoutTemplate>
        <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
    </LayoutTemplate>
    <ItemTemplate>
        <asp:CheckBox ID="CheckBoxSelect" runat="server" OnCheckedChanged="CheckBoxSelect_checkchanged"/>            
        <asp:LinkButton ID="LinkButtonOpen" CommandArgument='<%#Eval("MessageID") %>' runat="server">            
            &nbsp; &nbsp; &nbsp;
            <asp:Label ID="Label1" Text="[]" runat="server"/>
            &nbsp; &nbsp; &nbsp;
            <%#Eval("FirstName" )%>&nbsp;<%#Eval("LastName")%>
            &nbsp; &nbsp; &nbsp;
            <%#Eval("Subject") %>
            &nbsp; &nbsp; &nbsp;
            <%#Eval("Timestamp") %>
            <asp:HiddenField runat="server" ID="ReadStatus" Value='<%#Eval("IsRead") %>' />
        </asp:LinkButton>
    </ItemTemplate>
</asp:ListView>

Upvotes: 0

Views: 3110

Answers (2)

Akshay
Akshay

Reputation: 1706

The problem was that I was binding the ListView during page load. When that happened, the checkboxes would get cleared, and I got the Checked property as False in all subsequent functions.

Upvotes: 1

alejandrobog
alejandrobog

Reputation: 2101

I think you are missing the AutoPostBack="true"

<asp:CheckBox ID="CheckBoxSelect" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBoxSelect_checkchanged"/>       

Upvotes: 1

Related Questions