簡體   English   中英

GridView中的CheckBoxList僅記住添加新行時選中的第一個選項

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

我有一個帶有多列的網格視圖,允許用戶填寫數據,他們可以在填寫完數據后添加新行。 在各列之間,有一列帶有CheckBoxList的列,我允許用戶在CheckBoxList上多次選擇該選項,但是每次添加新行時,僅保留用戶選擇的第一個選項,而其他選擇消失。 添加新行時,如何保留用戶選擇的選項?

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

您需要維護所選復選框的狀態。 在按鈕上單擊“添加新行”,首先獲取數據表中各行的狀態,然后添加空白行,然后填充該數據表。

您還需要維護復選框的選定項目的狀態。 您可以將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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM