I am loading a set of records that load in a Repeater
control. I have a CheckBox
control for each record that determines whether the item is Active/Inactive. How do I loop through all the records in the Repeater
on a button click event and save the state of the CheckBox
? I will need to get the ID of the record and the Checked state of the control.
Thanks!
There's a few ways to approach it. If you are not re-binding the data on PostBack (e.g. you are relying on the already-populated repeater), you need to write the record ID to some field that will be persisted in ViewState. In this example I've used a HiddenField:
void Button_Click(object sender, EventArgs e)
{
foreach(RepeaterItem item in myRepeater.Items)
{
CheckBox cbxIsActive = item.FindControl("cbxID") as CheckBox;
HiddenField hdfID = item.FindControl("recordID") as HiddenField;
if(cbxIsActive != null && hdfID != null)
{
string recordID = hdfID.Value;
bool isActive = cbxIsActive.Checked;
UpdateRecord(recordID, isActive);
}
}
}