2016-07-08 115 views
0

我有一個gridview,它會維持複選框狀態,同時在gridview中分頁。爲什麼當我刷新頁面時,複選框仍然會保持狀態?如何在刷新頁面或向數據庫提交數據後禁用複選框狀態維護?禁用狀態維護

private void savechkdvls() 
{ 
    ArrayList usercontent = new ArrayList(); 
    int index = -1; 
    foreach (GridViewRow gvrow in GrdRole.Rows) 
    { 
     index = Convert.ToInt32(GrdRole.DataKeys[gvrow.RowIndex].Value); 
     bool result = ((CheckBox)gvrow.FindControl("chkSelect")).Checked; 
     if (Session["chkditems"] != null) 
      usercontent = (ArrayList)Session["chkditems"]; 
     if (result) 
     { 
      if (!usercontent.Contains(index)) 
       usercontent.Add(index); 
     } 
     else 
      usercontent.Remove(index); 
    } 
    if (usercontent != null && usercontent.Count > 0) 
     Session["chkditems"] = usercontent; 
} 
private void chkdvaluesp() 
{ 
    ArrayList usercontent = (ArrayList)Session["chkditems"]; 
    if (usercontent != null && usercontent.Count > 0) 
    { 
     foreach (GridViewRow gvrow in GrdRole.Rows) 
     { 
      int index = Convert.ToInt32(GrdRole.DataKeys[gvrow.RowIndex].Value); 
      if (usercontent.Contains(index)) 
      { 
       CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkSelect"); 
       myCheckBox.Checked = true; 
      } 
     } 
    } 
} 

if (!IsPostBack) 
    { 
     filldropdown(); 
     Bind(); 
    } 
+0

你的意思是,當你去到下一個頁面,該複選框具有與前一頁相同的值? – Ted

+0

現在我的問題是我刷新頁面後,我勾選的複選框以前仍然保持,我只想維護複選框,而我分頁gridview.any想法如何清除/禁用視圖狀態? – KyLim

+0

該命令很簡單,但我不確定你選擇的方式是否是最佳實踐: 'Viewstate [「name of control」] = null;' – Ted

回答

-1

你能夠使用

ViewState.Clear(); 
Response.Redirect("~/PageXXX.aspx"); 

清除的ViewState

+0

這樣你就可以用鍵「ViewState」清除Session變量。清除ViewState的命令是:'ViewState.Clear()' – Ted