如果取消选中复选框,则复选框值无法正确存储

时间:2015-12-04 21:56:40

标签: c# asp.net linq checkbox repeater

我在转发器中有每个任务的复选框。当我选中这些框时,true的值存储在数据库中,但是当我取消选中该复选框时,它不会正确存储false值。只有当我取消选中创建的最新复选框时,我才能在取消选中时更改其他人的false值。任何帮助或建议都会很棒。提前谢谢!

以下是转发器的代码隐藏:

    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();
        }
    }

0 个答案:

没有答案