GridView 中的 CheckBoxList 仅记住添加新行时勾选的第一个选项

CheckBoxList In GridView Only Remember The First Option Ticked When New Row Added

我有一个包含多列的网格视图,允许用户填写数据,他们可以在完成数据填写后添加新行。在这些列中,有一个带有 CheckBoxList 的列,我允许用户在 CheckBoxList 上添加多个 select 选项,但每次添加新行时,只有用户的第一个选项 select 保留,而其他select离子消失了。在我添加新行时,如何让用户 select 编辑的选项保留?

private void SetPreviousDataLecturer()
{
    int rowIndex = 0;
    if (ViewState["LecturerGridView"] != null)
    {
        DataTable dataTableCurrent = (DataTable)ViewState["LecturerGridView"];
        if (dataTableCurrent.Rows.Count > 0)
        {
            for (int i = 0; i < dataTableCurrent.Rows.Count; i++)
            {
                TextBox textBoxLName = (TextBox)LecturerGridView.Rows[rowIndex].Cells[1].FindControl("LecturerName");
                TextBox textBoxLID = (TextBox)LecturerGridView.Rows[rowIndex].Cells[2].FindControl("LecturerID");
                TextBox textBoxLAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[3].FindControl("LecturerAddress");
                TextBox textBoxLPNumber = (TextBox)LecturerGridView.Rows[rowIndex].Cells[4].FindControl("LecturerPNumber");
                TextBox textBoxLEAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[5].FindControl("LecturerEAddress");
                CheckBoxList checkBoxListLCourse = (CheckBoxList)LecturerGridView.Rows[rowIndex].Cells[6].FindControl("LecturerCourse");
                TextBox textBoxLPassword = (TextBox)LecturerGridView.Rows[rowIndex].Cells[7].FindControl("LecturerPassword");

                LecturerGridView.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                textBoxLName.Text = dataTableCurrent.Rows[i]["LecturerName"].ToString();
                textBoxLID.Text = dataTableCurrent.Rows[i]["LecturerID"].ToString();
                textBoxLAdd.Text = dataTableCurrent.Rows[i]["LecturerAddress"].ToString();
                textBoxLPNumber.Text = dataTableCurrent.Rows[i]["LecturerPNumber"].ToString();
                textBoxLEAdd.Text = dataTableCurrent.Rows[i]["LecturerEAddress"].ToString();
                checkBoxListLCourse.SelectedValue = dataTableCurrent.Rows[i]["LecturerCourse"].ToString();
                textBoxLPassword.Text = dataTableCurrent.Rows[i]["LecturerPassword"].ToString();
                rowIndex++;
            }
        }
    }
}

private void AddNewRowToLecturerGV()
{
    int rowIndex = 0;
    if (ViewState["LecturerGridView"] != null)
    {
        DataTable dataTableCurrent = (DataTable)ViewState["LecturerGridView"];
        DataRow dataRowCurrent = null;
        if (dataTableCurrent.Rows.Count > 0)
        {
            for (int i = 1; i <= dataTableCurrent.Rows.Count; i++)
            {
                TextBox textBoxLName = (TextBox)LecturerGridView.Rows[rowIndex].Cells[1].FindControl("LecturerName");
                TextBox textBoxLID = (TextBox)LecturerGridView.Rows[rowIndex].Cells[2].FindControl("LecturerID");
                TextBox textBoxLAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[3].FindControl("LecturerAddress");
                TextBox textBoxLPNumber = (TextBox)LecturerGridView.Rows[rowIndex].Cells[4].FindControl("LecturerPNumber");
                TextBox textBoxLEAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[5].FindControl("LecturerEAddress");
                CheckBoxList checkBoxListLCourse = (CheckBoxList)LecturerGridView.Rows[rowIndex].Cells[6].FindControl("LecturerCourse");
                TextBox textBoxLPassword = (TextBox)LecturerGridView.Rows[rowIndex].Cells[7].FindControl("LecturerPassword");

                dataRowCurrent = dataTableCurrent.NewRow();
                dataRowCurrent["RowNumber"] = i + 1;
                dataTableCurrent.Rows[i - 1]["LecturerName"] = textBoxLName.Text;
                dataTableCurrent.Rows[i - 1]["LecturerID"] = textBoxLID.Text;
                dataTableCurrent.Rows[i - 1]["LecturerAddress"] = textBoxLAdd.Text;
                dataTableCurrent.Rows[i - 1]["LecturerPNumber"] = textBoxLPNumber.Text;
                dataTableCurrent.Rows[i - 1]["LecturerEAddress"] = textBoxLEAdd.Text;
                dataTableCurrent.Rows[i - 1]["LecturerCourse"] = checkBoxListLCourse.SelectedValue.ToString();
                dataTableCurrent.Rows[i - 1]["LecturerPassword"] = textBoxLPassword.Text;

                rowIndex++;
            }

            dataTableCurrent.Rows.Add(dataRowCurrent);
            ViewState["LecturerGridView"] = dataTableCurrent;

            LecturerGridView.DataSource = dataTableCurrent;
            LecturerGridView.DataBind();
        }
    }
    else
    {
        Response.Write("ViewState is null.");
    }
    SetPreviousDataLecturer();
}

您需要维护选中复选框的状态。在按钮上单击 'Add new row' 首先获取 DataTable 中每一行的状态并添加一个空白行,然后填充该 DataTable。

您还需要维护复选框的选中项的状态。您可以在 CSV 中获取选定的值:

string selectedItems = String.Join(",",
checkBoxListLCourse.Items.OfType<ListItem>().Where(r => r.Selected)
    .Select(r => r.Value));

您可以恢复为:

        string[] items = selectedItems.Split(',');
        for (int i = 0; i < checkBoxListLCourse.Items.Count; i++)
         {
            if (items.Contains(checkBoxListLCourse.Items[i].Value))
            {
              checkBoxListLCourse.Items[i].Selected = true;
            }
         }

我的回答。这个答案有一些问题,比如当我们勾选复选框列表中的任何内容时,复选框列表会自动滚动到最顶部。

private void SetPreviousDataLecturer()
{
    int rowIndex = 0;

    if (ViewState["LecturerGridView"] != null)
    {
        DataTable dataTableCurrent = (DataTable)ViewState["LecturerGridView"];
        if (dataTableCurrent.Rows.Count > 0)
        {
            for (int i = 0; i < dataTableCurrent.Rows.Count; i++)
            {
                TextBox textBoxLName = (TextBox)LecturerGridView.Rows[rowIndex].Cells[1].FindControl("LecturerName");
                TextBox textBoxLID = (TextBox)LecturerGridView.Rows[rowIndex].Cells[2].FindControl("LecturerID");
                TextBox textBoxLAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[3].FindControl("LecturerAddress");
                TextBox textBoxLPNumber = (TextBox)LecturerGridView.Rows[rowIndex].Cells[4].FindControl("LecturerPNumber");
                TextBox textBoxLEAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[5].FindControl("LecturerEAddress");
                CheckBoxList checkBoxListLCourse = (CheckBoxList)LecturerGridView.Rows[rowIndex].Cells[6].FindControl("LecturerCourse");
                TextBox textBoxLPassword = (TextBox)LecturerGridView.Rows[rowIndex].Cells[7].FindControl("LecturerPassword");

                LecturerGridView.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                textBoxLName.Text = dataTableCurrent.Rows[i]["LecturerName"].ToString();
                textBoxLID.Text = dataTableCurrent.Rows[i]["LecturerID"].ToString();
                textBoxLAdd.Text = dataTableCurrent.Rows[i]["LecturerAddress"].ToString();
                textBoxLPNumber.Text = dataTableCurrent.Rows[i]["LecturerPNumber"].ToString();
                textBoxLEAdd.Text = dataTableCurrent.Rows[i]["LecturerEAddress"].ToString();
                checkBoxListLCourse.Text = dataTableCurrent.Rows[i]["LecturerCourse"].ToString();
                string lecturerCourse = dataTableCurrent.Rows[i]["LecturerCourse"].ToString();
                if (!string.IsNullOrEmpty(lecturerCourse))
                {
                    for (int j = 0; j < lecturerCourse.Split(',').Length; j++)
                    {
                        checkBoxListLCourse.Items.FindByValue(lecturerCourse.Split(',')[j].ToString()).Selected = true;
                    }
                }
                textBoxLPassword.Text = dataTableCurrent.Rows[i]["LecturerPassword"].ToString();
                rowIndex++;
            }
        }
    }
}

private void AddNewRowToLecturerGV()
{
    int rowIndex = 0;

    if (ViewState["LecturerGridView"] != null)
    {
        DataTable dataTableCurrent = (DataTable)ViewState["LecturerGridView"];
        DataRow dataRowCurrent = null;
        if (dataTableCurrent.Rows.Count > 0)
        {
            for (int i = 1; i <= dataTableCurrent.Rows.Count; i++)
            {
                TextBox textBoxLName = (TextBox)LecturerGridView.Rows[rowIndex].Cells[1].FindControl("LecturerName");
                TextBox textBoxLID = (TextBox)LecturerGridView.Rows[rowIndex].Cells[2].FindControl("LecturerID");
                TextBox textBoxLAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[3].FindControl("LecturerAddress");
                TextBox textBoxLPNumber = (TextBox)LecturerGridView.Rows[rowIndex].Cells[4].FindControl("LecturerPNumber");
                TextBox textBoxLEAdd = (TextBox)LecturerGridView.Rows[rowIndex].Cells[5].FindControl("LecturerEAddress");
                CheckBoxList checkBoxListLCourse = (CheckBoxList)LecturerGridView.Rows[rowIndex].Cells[6].FindControl("LecturerCourse");
                TextBox textBoxLPassword = (TextBox)LecturerGridView.Rows[rowIndex].Cells[7].FindControl("LecturerPassword");

                dataRowCurrent = dataTableCurrent.NewRow();
                dataRowCurrent["RowNumber"] = i + 1;
                dataTableCurrent.Rows[i - 1]["LecturerName"] = textBoxLName.Text;
                dataTableCurrent.Rows[i - 1]["LecturerID"] = textBoxLID.Text;
                dataTableCurrent.Rows[i - 1]["LecturerAddress"] = textBoxLAdd.Text;
                dataTableCurrent.Rows[i - 1]["LecturerPNumber"] = textBoxLPNumber.Text;
                dataTableCurrent.Rows[i - 1]["LecturerEAddress"] = textBoxLEAdd.Text;
                string lecturerCourse = string.Empty;
                foreach (ListItem item in checkBoxListLCourse.Items)
                {
                    if (item.Selected)
                    {
                        if (!string.IsNullOrEmpty(lecturerCourse))
                        {
                            lecturerCourse += ",";
                        }
                        lecturerCourse += item.Value;
                    }
                }
                dataTableCurrent.Rows[i - 1]["LecturerCourse"] = lecturerCourse;
                dataTableCurrent.Rows[i - 1]["LecturerPassword"] = textBoxLPassword.Text;
                rowIndex++;
            }
            dataTableCurrent.Rows.Add(dataRowCurrent);
            ViewState["LecturerGridView"] = dataTableCurrent;

            LecturerGridView.DataSource = dataTableCurrent;
            LecturerGridView.DataBind();
        }
    }
    else
    {
        Response.Write("ViewState is null.");
    }
    SetPreviousDataLecturer();
}