2012-11-29 130 views
0

這裏是我的viewstate的代碼。但它只存儲一個值。我需要的是它會將選定的多個值保留在複選框中。這種方法是在分頁情況的gridview中保持/保留複選框的值。如何在viewstate中存儲複選框值的多個值?

public void chkAssignee_OnCheckedChanged(object sender, EventArgs e) 
     { 
     CheckBox selectBox = (CheckBox)sender; 
     GridViewRow myRow = (GridViewRow)selectBox.Parent.Parent; // the row 
     GridView myGrid = (GridView)myRow.Parent.Parent; // the gridview 
     string ID = myGrid.DataKeys[myRow.RowIndex].Value.ToString(); 
     GridViewRow rowSelect = (GridViewRow)selectBox.Parent.Parent; 

     int a = rowSelect.RowIndex; 

     ViewState["id"] = ID; 
     } 
+0

嘗試將其保存到逗號分隔字符串或東西。 – sajanyamaha

+0

將內容保存在「List」或「Dictionary」中,並保存到「ViewState」中。 –

回答

1

嘗試以下 以下代碼可能會幫助你。


public void chkAssignee_OnCheckedChanged(object sender, EventArgs e) 
    { 
     CheckBox selectBox = (CheckBox)sender; 
     GridViewRow myRow = (GridViewRow)selectBox.Parent.Parent; // the row 
     GridView myGrid = (GridView)myRow.Parent.Parent; // the gridview 
     string ID = myGrid.DataKeys[myRow.RowIndex].Value.ToString(); 
     GridViewRow rowSelect = (GridViewRow)selectBox.Parent.Parent;

int a = rowSelect.RowIndex; 
    ArrayList SelecterdRowIndices=new ArrayList(); 
    if(ViewState["SelectedRowIndices"]!=null) 
    { 
     SelecterdRowIndices=(ArrayList)ViewState["SelectedRowIndices"]; 
     bool flag=false; 
     foreach (int i in SelecterdRowIndices) 
     { 
      if(i==Convert.ToInt32(ID)) 
      { 
       flag=true; 
       break; 
      } 
     } 
     if(!flag) 
     { 
      SelecterdRowIndices.Add(ID); 
     } 
    } 
    else 
    { 
     SelecterdRowIndices.Add(ID); 
    } 
    ViewState["SelectedRowIndices"] = SelecterdRowIndices; 

}