2014-07-14 39 views
2

我在啓用了分頁的aspx頁面上有一個gridview。 該gridview包含數據庫的一些數據字段和每行的複選框。在GridView控件中分頁時維護狀態和複選框的計數

我開始想知道如果在循環遍歷所有行之前重新綁定數據源,是否會記住複選框選項,但很快確定即使從一個頁面轉到下一個頁面,然後再次返回複選框選項丟失了。

要堅持的複選框選中狀態我試圖在本教程中自定義實現:http://aspalliance.com/774_Maintaining_State_of_CheckBoxes_While_Paging_in_a_GridView_Control.all

我想指望這是我的asp.net頁面上選中的複選框的數量,如果數量= 5則將按鈕狀態從禁用改爲啓用,但是當我在Gridview中更改頁面時,不會計算上一頁中網格的選定行。

我的代碼如下。

我將不勝感激任何幫助,您可以給我在工作的這個問題。

private void RememberOldValues() 
{ 
    ArrayList categoryIDList = new ArrayList(); 
    int index = -1; 
    foreach (GridViewRow row in GridView1.Rows) 
    { 
     index = (int)GridView1.DataKeys[row.RowIndex].Value; 
     bool result = ((CheckBox)row.FindControl("chkSelect")).Checked; 

     if (Session["CHECKED_ITEMS"] != null) 
      categoryIDList = (ArrayList)Session["CHECKED_ITEMS"]; 
     if (result) 
     { 
      if (!categoryIDList.Contains(index)) 
       categoryIDList.Add(index); 
     } 
     else 
      categoryIDList.Remove(index); 
    } 
    if (categoryIDList != null && categoryIDList.Count > 0) 
     Session["CHECKED_ITEMS"] = categoryIDList; 
} 

private void RePopulateValues() 
{ 
    ArrayList categoryIDList = (ArrayList)Session["CHECKED_ITEMS"]; 
    if (categoryIDList != null && categoryIDList.Count > 0) 
    { 
     foreach (GridViewRow row in GridView1.Rows) 
     { 
      int index = (int)GridView1.DataKeys[row.RowIndex].Value; 
      if (categoryIDList.Contains(index)) 
      { 
       CheckBox myCheckBox = (CheckBox)row.FindControl("chkSelect"); 
       myCheckBox.Checked = true; 
      } 
     } 
    } 
} 



protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    RememberOldValues(); 
    GridViewBind(); 
    GridView1.DataSource = dset.Tables[0]; 
    GridView1.PageIndex = e.NewPageIndex; 
    GridView1.DataBind(); 
    RePopulateValues(); 
} 


protected void CheckBox1_CheckedChanged(object sender, EventArgs e) 
{ 

    CheckBox chkTest = (CheckBox)sender; 
    GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer; 

    int count = 0; 
    foreach (GridViewRow row in GridView1.Rows) 
    { 
     CheckBox chk = (CheckBox)row.FindControl("chkSelect"); 
     if (chk.Checked) 
     { 
      count++; 
      grdRow.BackColor = System.Drawing.Color.Yellow; 
     } 
    } 

    if (count == 5) 
    { 
     btnUpdate.Enabled = true; 
     btnUpdate.CssClass = "enabledImageButton"; 
    } 
    else 
    { 
     btnUpdate.Enabled = false; 
     btnUpdate.CssClass = "disabledImageButton"; 
    } 
} 

回答

0

聲明一個私有屬性來從會話中讀取arralist,以避免一次又一次地調用它。

ArrayList SelectedCategories 
{ 
    get 
    { 
     ArrayList categoryIDList; 
     if (Session["CHECKED_ITEMS"] != null) 
      categoryIDList = (ArrayList)Session["CHECKED_ITEMS"]; 
     else 
     { 
      categoryIDList = new ArrayList(); 
      Session["CHECKED_ITEMS"] = categoryIDList; 
     } 
     return categoryIDList; 
    } 
} 

然後在您的複選框更改事件中,您可以更改代碼以訪問存儲的選擇數組列表。

protected void CheckBox1_CheckedChanged(object sender, EventArgs e) 
{ 

    CheckBox chkTest = (CheckBox)sender; 
    GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer; 
    int index = (int)GridView1.DataKeys[grdRow.RowIndex].Value; 
    if (chkTest.Checked) 
    { 
     if (!SelectedCategories.Contains(index)) 
      SelectedCategories.Add(index); 
     grdRow.BackColor = System.Drawing.Color.Yellow; 
    } 
    else 
    { 
     if (SelectedCategories.Contains(index)) 
      SelectedCategories.Remove(index); 
     grdRow.BackColor = System.Drawing.Color.White; 
    } 
    if (SelectedCategories.Count >= 5) 
    { 
     btnUpdate.Enabled = true; 
     btnUpdate.CssClass = "enabledImageButton"; 
    } 
    else 
    { 
     btnUpdate.Enabled = false; 
     btnUpdate.CssClass = "disabledImageButton"; 
    } 
} 
+0

非常感謝! – Hamamelis

相關問題