GridView中的CheckBoxList只记住添加新行时勾选的第一个选项 [英] CheckBoxList In GridView Only Remember The First Option Ticked When New Row Added

查看:156
本文介绍了GridView中的CheckBoxList只记住添加新行时勾选的第一个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有多列的网格视图,允许用户填写数据,并且可以在完成填充数据后添加新行。在这些列中,有一列带有CheckBoxList的列,我允许用户在CheckBoxList上多选择一个选项,但每次添加一个新行时,只有用户选择的第一个选项保留,而其他选择不存在。我添加一个新行时,如何让用户选择的选项保持不变?

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


推荐答案

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

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

这篇关于GridView中的CheckBoxList只记住添加新行时勾选的第一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆