代码之家  ›  专栏  ›  技术社区  ›  Yih Wei

GridView中的复选框列表只记得添加新行时勾选的第一个选项

  •  0
  • Yih Wei  · 技术社区  · 7 年前

    我有一个包含多列的网格视图,允许用户填写数据,他们可以在填写完数据后添加新行。在这些列中,有一个带有复选框列表的列,我允许用户在复选框列表上多次选择选项,但每次添加新行时,只有用户选择的第一个选项保留,而其他选择则消失。当我添加新行时,如何让用户选择的选项保持不变?

    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();
    }
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Mohammad Arshad Alam    7 年前

    您需要维护选中复选框的状态。在按钮上单击“添加新行”,首先获取数据表中每行的状态,并添加一个空行,然后填充该数据表。

    您还需要维护复选框的选定项的状态。您可以通过以下方式获得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;
                }
             }
    
        2
  •  0
  •   Yih Wei    7 年前

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

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