vendredi 4 décembre 2015

Check box value is not storing properly when check box is unchecked

I have check boxes for each task in a repeater. When I check the boxes, the value of true is stored in the database but when I uncheck the check box, it does not store the value of false properly. Only when I uncheck the latest check box created, then I can change value of false of the others when I uncheck it. Any help or suggestions would be great. Thanks in advance!

Here is the code-behind to the repeater:

    protected void rptTask_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HyperLink hlTask = (HyperLink)e.Item.FindControl("hlTask");
            CheckBox cboxTask = (CheckBox)e.Item.FindControl("cboxTask");
            Task task = e.Item.DataItem as Task;
            cboxTask.InputAttributes.Add("TaskId", task.TaskId.ToString());
        }
    }

    protected void TaskCheckBoxChanged(object sender, EventArgs e)
    {
        var taskId = Convert.ToInt32((sender as CheckBox).InputAttributes["TaskId"]);

        using (TaskManagerEntities myEntities = new TaskManagerEntities())
        {
            Task task;

            task = (from t in myEntities.Tasks
                    where t.TaskId == taskId
                    select t).SingleOrDefault();

            foreach (RepeaterItem item in rptTask.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    CheckBox cboxTask = (CheckBox)item.FindControl("cboxTask");

                    task.Completed = cboxTask.Checked;
                }
            }

            myEntities.SaveChanges();
        }
    }




Aucun commentaire:

Enregistrer un commentaire