Join 150,402 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 966 people online right now. Registration is fast and FREE... Join Now!
So as you can clearly see cbSelect does exist. I then have 2 methods, one for getting the checked items in the list (to hold them during paging) and one to repopulate the checked items if the user goes back to that page. To get the checked items I have
csharp
private void GetCheckedItems() { //instantiate new ArrayList to hold our checked items checkedItems = new ArrayList(); //loop through each row in the GridView foreach (GridViewRow row in gvUserQs.Rows) { //get the index of the current CheckBox chkBoxIndex = (long)gvUserQs.DataKeys[row.RowIndex].Value; CheckBox chk = (CheckBox)gvUserQs.FindControl("cbSelect");
//add ArrayList to Session is if doesnt already exist if (!(Session["CheckedItems"] == null)) { checkedItems = (ArrayList)Session["CheckedItems"]; }
//now see if the current CheckBox is checked if (chk.Checked) //<-- Exception happens here { bxChkd = true; //see if the current value is in the Session, if not add it if (!(checkedItems.Contains(chkBoxIndex))) { //add to the list checkedItems.Contains(chkBoxIndex.ToString()); } } else { //remove from list since it's unchecked checkedItems.Remove(chkBoxIndex.ToString()); } } //update Session with the list of checked items Session["CheckedItems"] = chkdItems; }
//make sure we have selected items if (!(checkedItems == null)) { foreach (GridViewRow row in gvUserQs.Rows) { chkBoxIndex = (long)gvUserQs.DataKeys[row.RowIndex].Value;
I figured out the problem (but that now causes another problem). I had to change mny method to look like
csharp
private void GetCheckedItems() { //instantiate new ArrayList to hold our checked items checkedItems = new ArrayList(); //loop through each row in the GridView foreach (GridViewRow row in gvUserQs.Rows) { //get the index of the current CheckBox chkBoxIndex = (long)gvUserQs.DataKeys[row.RowIndex].Value; //here I was using gvUserQs when I needed to be using row CheckBox chk = (CheckBox)row.FindControl("cbSelect"); //add ArrayList to ViewState is if doesnt already exist if (!(Session["CheckedItems"] == null)) { checkedItems = (ArrayList)Session["CheckedItems"]; }
//now see if the current CheckBox is checked if (chk.Checked) { bxChkd = true; //see if the current value is in the ViewState, if not add it if (!(checkedItems.Contains(chkBoxIndex))) { //add to the list checkedItems.Contains(chkBoxIndex.ToString()); } } else { //remove from list since it's unchecked checkedItems.Remove(chkBoxIndex.ToString()); } } //update ViewState with the list of checked items Session["CheckedItems"] = chkdItems; }
Now the problem is when I go back to the new page I get an
QUOTE
Exception Details: System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Collections.ArrayList'.
In this block of code
csharp
//add ArrayList to ViewState is if doesnt already exist if (!(Session["CheckedItems"] == null)) { checkedItems = (ArrayList)Session["CheckedItems"]; //<-- Here }
Now to my eye it looks like this code should work, what am I missing?
Update: got rid of all errors (I had the wrong variable at the bottom of the GetCheckedItems method), but the RePopulateCheckedItems isn't working, it isn't checking the items that were initially checked on a page, and it doesn't appear to be adding checked items to the ArrayList (getCheckedItems method)
Well I believe I got it adding checked items to the ArrayList, but now I'm getting inconsistent results. Sometimes when I go back to a page the items I checked are indeed checked again, but the next time I go they aren't. Let me show all the code and explain what it is doing.
Now this first method is used for cycling through the items in the GridView (for that page number) and adding each checked item to an ArrayList, the ArrayList is then added to the Session["CheckedItems"] so I can keep track of them through the paging process
csharp
private void GetCheckedItems() { //instantiate new ArrayList to hold our checked items checkedItems = new ArrayList(); //loop through each row in the GridView foreach (GridViewRow row in gvUserQs.Rows) { //get the index of the current CheckBox chkBoxIndex = (string)gvUserQs.DataKeys[row.RowIndex].Value.ToString(); chk = (CheckBox)row.FindControl("cbSelect"); //add ArrayList to Session is if doesnt already exist if (!(Session["CheckedItems"] == null)) { checkedItems = (ArrayList)Session["CheckedItems"]; }
//now see if the current CheckBox is checked if (chk.Checked) { bxChkd = true; //see if the current value is in the Session, if not add it if (!(checkedItems.Contains(chkBoxIndex))) { //add to the list checkedItems.Add(chkBoxIndex); } else { //remove from list since it's unchecked checkedItems.Remove(chkBoxIndex); } }
} //update Session with the list of checked items Session["CheckedItems"] = checkedItems; }
This next method is used for rechecking items when/if the user pages back to page they've already been to
//make sure we have selected items if (!(checkedItems == null)) { foreach (GridViewRow row in gvUserQs.Rows) { chkBoxIndex = (string)gvUserQs.DataKeys[row.RowIndex].Value.ToString();
You shouldn't need to handle anything in the grid when using the paging property. The grid should be updating automatically with the correct values. Is this not the case?
It is refreshing the data, I'm wanting to keep track of the items a user selects while they're paging. Then if they page back to a page they've been to I want it to check the items they had checked on that page. I think I have it working now, found a couple small errors in my code.