Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save items loaded in a repeater?

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!

like image 732
Mike Cole Avatar asked Sep 28 '09 16:09

Mike Cole


1 Answers

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);
        }
    }
}
like image 91
Rex M Avatar answered Nov 01 '22 19:11

Rex M