简体   繁体   English

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

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

I have a grid view with multiple columns which allow user to fill in the data and they are able to add a new row after finishing filling the data. 我有一个带有多列的网格视图,允许用户填写数据,他们可以在填写完数据后添加新行。 Among the columns, there is a column with CheckBoxList which I allow user to multiple select the option on the CheckBoxList but every time add a new row, only the first option select by the user is remain while other selection is gone. 在各列之间,有一列带有CheckBoxList的列,我允许用户在CheckBoxList上多次选择该选项,但是每次添加新行时,仅保留用户选择的第一个选项,而其他选择消失。 How am I able to let the option selected by the user remain while I add a new row? 添加新行时,如何保留用户选择的选项?

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

You need to maintain state for selected checkbox. 您需要维护所选复选框的状态。 On the button click 'Add new row' first get the state of each rows in a DataTable and add a blank row then populate that DataTable. 在按钮上单击“添加新行”,首先获取数据表中各行的状态,然后添加空白行,然后填充该数据表。

You need to maintain checkbox's selected item's state also. 您还需要维护复选框的选定项目的状态。 You can get selected values in a CSV as : 您可以将CSV中的选定值获取为:

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

and you can restore as : 您可以还原为:

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

My answer. 我的答案。 This answer has some problem like the checkbox list will automatically scroll to the most top when we tick on anything in the checkbox list. 这个答案有一些问题,例如当我们在复选框列表中打勾时,复选框列表会自动滚动到最顶部。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM