'Save the checked status of a CheckBox included inside an ASP ListView

I know there are similar questions about this issue (for example, here and here) but no one results helpful for my problem.

I have a ListView control showing all the users registered in the database and there is a CheckBox for each user shown if the user is approved or not, and I wanna save the changes directly when the CheckBox's Checked property changes.

I know it's not correct to add the event handler on the ListView_ItemDataBound, because after the CheckBox's AutoPostback there is not a new binding, thus the event handler get lost. On the other hand, I can't append the method directly on the ASPX file because this way I can't know which user is affected by the change (at least, I think I can't).

Any sugestion?

Thanks a lot



Solution 1:[1]

So i ran into the same problem. i'm curious, on your page load are you checking if its a postback?

if(!Post.IsPostBack){
//normal page load
}

If you don't have that check, it will call your page load logic, in my case it was resetting the checkbox everytime with my data object.

Solution 2:[2]

On the aspx on the checkbox OnCheckedChanged="ckbNameOfCheckbox_CheckedChanged" AutoPostBack="true"

In the code behind

Protected Sub ckbNameOfCheckbox_CheckedChanged(sender As Object, e As EventArgs)

    Dim chkBox As CheckBox = CType(sender, CheckBox)

    ' Gets the item that contains the CheckBox object. 
    Dim item As ListViewDataItem = CType(chkBox.Parent, ListViewDataItem)



    NameOfTheListView.UpdateItem(item.DisplayIndex, sender.Checked)
End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jhuang
Solution 2