Bengie
Bengie

Reputation: 1035

Referencing Checkboxes within a Repeater

I have a Repeater control that is creating a dynamic amount of CheckBoxList controls and each list has a different set of ListItems.

I have done this part just fine, but the issue I'm having is how to save the checked states of these dynamically created boxes. I cannot find out how to get the list of these CheckBoxList controls.

Here's some pseudo-code of what I'm trying to do:

foreach (Item i in MyRepeater)
{
    if (i.ItemType is CheckBoxList)
    {
        foreach (ListItem x in i)
        {
            update table set tiChecked = x.Checked
            where table.id = i.id and table.typeid = x.id
        }
    }
}

I have the ID of the CheckBoxList and the ListItem corresponding to the IDs in the DB.

Edit:

Of course after I ask, I find it out. This seems to be getting me what I want

foreach (RepeaterItem tmp in rptReportList.Items)
{
    if (tmp.ItemType == ListItemType.Item)
    {
        foreach (Control c in tmp.Controls)
        {
            if (c is CheckBoxList)
            {
                DisplayMessage(this, c.ID.ToString());
            }
        }
    }
}

Upvotes: 0

Views: 893

Answers (1)

Kelsey
Kelsey

Reputation: 47776

You need to look deeper than the RepeaterItem:

// repeater item
foreach (Control cr in MyRepeater.Controls)
{
    // controls within repeater item
    foreach (Control c in cr.Controls)
    {
        CheckBoxList chklst = c as CheckBoxList;
        if (chklst != null)
        {
            foreach (ListItem i in chklst.Items)
            {
                string valueToUpdate = i.Value;
                string textToUpdate = i.Text;
                bool checkedToUpdate = i.Selected;

                // Do update
            }
        }
    }
}

Upvotes: 3

Related Questions