I hope someone could help me with this. I wasn’t able to find the ImageButton control inside the ListView, it always gives me Object reference not set to an instance of an object error.
The scenario is, if I checked the checkbox, I wanted the imagebutton to change its’ image. The Checkbox does not reside inside the Listview ItemTemplate. Here’s my code behind for the checkbox_checkchanged event:
if (cb.Checked)
{
foreach (Control c in lv.Controls)
{
ImageButton btndel = (ImageButton)c.FindControl("btnDelete");
btndel.ImageUrl = "~/images/activate.png";
}
}
Note: I used ForEach Loop thinking that the btnDelete button appears several times in my Listview.
If the Checkbox is outside of the ListView, the best way would be to use ListView’s ItemCreated-Event:
You don’t need to handle the Checkbox’ CheckedChanged-Event but only need to add the OnItemCreated-handler on your aspx markup:
On this way you prevent multiple iterations of the ListView-Items. ItemCreated will anyway be called implicitely on every postback to recreate the ListView-Items.