views:

1218

answers:

2

I have a ListView with an EditItemTemplate that calls a method onItemEditing.

Within my ListView I have a CheckBoxList bound using LINQ.

In my onItemEditing method, I'm trying to check certain CheckBoxes if they are present in a look up table that links users with sectors.

However, when I load the EditItemTemplate none of the CheckBoxes are checked even though I've set them as selected in the onItemEditing method.

Here's the Method:

protected void onItemEditing(object sender, ListViewEditEventArgs e)
{
    ListView1.EditIndex = e.NewEditIndex;
    ListView1.DataBind();

    int regId = Convert.ToInt32(((Label)ListView1.Items[e.NewEditIndex].FindControl("LblRegId")).Text);
    CheckBoxList cbl = (CheckBoxList) ListView1.Items[e.NewEditIndex].FindControl("chkLstSectors");

//test to see if forcing first check box to be selected works - doesn't work
    cbl.Items[0].Selected = true;

    SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DaresburyConnectionString"].ToString());
    SqlCommand objCmd = new SqlCommand("select * from register_sectors where register_id= " + regId, objConn);
    objConn.Open();

    SqlDataReader objReader = objCmd.ExecuteReader();

    if (objReader != null)
    {
        while (objReader.Read())
        {
            ListItem currentCheckBox = cbl.Items.FindByValue(objReader["sector_id"].ToString());
            if (currentCheckBox != null)
            {
                currentCheckBox.Selected = true;
            }
        }
    }
}

Any ideas how to get around this?

A: 

Where did you create your control ? On load, on init ?

Cédric Boivin
A: 

Hi Guys,

The problem was the listView was being bound again after the checkboxlist was bound.

I removed the binding and it works!

mancmanomyst