2017-10-11 44 views
0

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

请问您可以发布完整的代码,包括aspx文件 – Arshad

+0

嗨。 @Arshad我已经包含了aspx和aspx.cs.请让我知道你是否需要更多。谢谢。 –

+0

它是完整的.cs文件吗?请包括完整的.cs文件,以便我可以查看您的页面加载事件也 – Arshad

回答

0

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

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

您需要维护所选复选框的状态。在按钮上单击“添加新行”,首先获取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; 
      } 
     } 
+0

嗨。 @Arshad谢谢你的回复。感到很抱歉问,但我应该把你的答案放在哪里? –

相关问题