0

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

Community
  • 1
  • 1
adripanico
  • 1,026
  • 14
  • 32

3 Answers3

1

You have a couple of options.

It's possible that the ListView.ItemCommand event might fire. I'm not sure, though, because the documentation just specifies buttons. You might want to experiment.

The other option would be to harness the ListView.ItemCreated command. I believe that this always runs, regardless of whether the ListView is bound or not, because the item always has to be created, even if it's from ViewState. What you would do in the event handler for that event would be to attach an event handler to the CheckBox Click or CheckChanged event (I forget what the server-side name is for a CheckBox state change event).

Ann L.
  • 13,760
  • 5
  • 35
  • 66
  • It seems your first approach does not work. Indeed, the `ListView.ItemCommand` event is not throw because a `CheckBox`'s `CheckedChanged`. – adripanico Sep 24 '12 at 09:35
  • The second options works! I can add the handler from the `ListView.ItemCreated`. The only problem now is how to know which user corresponds the `CheckBox` (I can use the `Text` property, but it shows the text in screen and I don't want that). – adripanico Sep 24 '12 at 10:11
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.

jhuang
  • 21
  • 3
-1

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
fab
  • 1
  • 1