2017-10-11 56 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謝謝你的回覆。感到很抱歉問,但我應該把你的答案放在哪裏? –

相關問題