I have a gridview bound to a SQL table. I have added a column to the gridview that is a template field containing a checkbox for each row. The checkbox is not bound. The purpose of the checkbox is to allow checking several rows in the gridview and then
clicking on some other button that will take action against every checked row (specifically, to delete from the SQL table any rows that are checked). Pretty common scenario...
Each row in the gridview also has a column with a "Modify" Commandfield in it, to allow the user to change a couple of the fields that are in the SQL table for that row.
My problem is the scenario where the user checks a bunch of the checkboxes, then clicks "Modify" to change the data in one of the unchecked rows. All of the checked checkboxes lose their checked status after the Modify has done its thing. So in other words,
the user checks a bunch of rows, then meanders off to do a Modify before clicking on the button that would take the delete action against all the checked rows. But there aren't any checked rows anymore, because the postback to the server from the Modify caused
all the checks to go away. Any way around this?
Make sure to only data-bind your GridView to its DataSource on the initial page load. If you are data-binding your GridView to its DataSource each and every PostBack, then you will lose the state of your CheckBoxes.
I'm not doing the databinding for the gridview (gridview.databind()) in my programming code, ASP is doing it for me. I presume you're suggesting I need to to it manually in my code...
Am I wrong in thinking that there HAS to be a Databind when a "Modify" Commandfield is clicked or the subsequent "Save" button is clicked? Wouldn't ASP need to Databind in order to display the just-modified values in the refreshed gridview?
After the update has happened, then you SHOULD re-bind your GridView to update the data. However, make sure you DO NOT re-bind your GridView each and every PostBack as this can cause havoc on your state.
But the thing is, I did not write any of the update code. The "Modify" link for each row is an ASP.Net CommandField that when clicked, shows a textbox for each modifiable field in that row along with a "Save" and "Cancel" link (all automatic stuff, built
into ASP.Net). When "Save" is clicked, ASP.Net somehow does the update via the UpdateCommand in the SQLDataSource that drives the gridview. Clicks on both "Modify" and "Save" cause postbacks to the page. So how would I know if the postback was caused by
a click on "Modify" or "Save"?
BTW, many thanks for your replies. There are no other ASP programmers here where I work, so the Internet is my only lifeline...
Ok, that makes sense now. When your Update occurs, your SqlDataSource performs the Update, and then again performs your SelectCommand. This, is what is wiping out your CheckBox state. What you will need to do is to save the state of your CheckBoxes within
the Session or ViewState. This can be done a variety of ways, but basically everytime a CheckBox is checked, you must save its state somewhere that it is now Checked or Unchecked. Then after every re-bind of your data, you can pull this state out of the Session
or ViewState and re-set the state of the CheckBoxes. Here's an example of how I would maintain the state:
Thanks Ed. I'm a VB guy so it will take me a little while to digest your C# codebehind, but I'll give your technique a try as soon as I'm able. I appreciate the help!
Kerminator
Member
5 Points
28 Posts
Gridview Checkbox state being lost
Jan 13, 2009 03:59 PM|LINK
I have a gridview bound to a SQL table. I have added a column to the gridview that is a template field containing a checkbox for each row. The checkbox is not bound. The purpose of the checkbox is to allow checking several rows in the gridview and then clicking on some other button that will take action against every checked row (specifically, to delete from the SQL table any rows that are checked). Pretty common scenario...
Each row in the gridview also has a column with a "Modify" Commandfield in it, to allow the user to change a couple of the fields that are in the SQL table for that row.
My problem is the scenario where the user checks a bunch of the checkboxes, then clicks "Modify" to change the data in one of the unchecked rows. All of the checked checkboxes lose their checked status after the Modify has done its thing. So in other words, the user checks a bunch of rows, then meanders off to do a Modify before clicking on the button that would take the delete action against all the checked rows. But there aren't any checked rows anymore, because the postback to the server from the Modify caused all the checks to go away. Any way around this?
ecbruck
All-Star
98772 Points
9690 Posts
Moderator
MVP
Re: Gridview Checkbox state being lost
Jan 13, 2009 04:16 PM|LINK
Make sure to only data-bind your GridView to its DataSource on the initial page load. If you are data-binding your GridView to its DataSource each and every PostBack, then you will lose the state of your CheckBoxes.
Microsoft MVP - ASP.NET
Kerminator
Member
5 Points
28 Posts
Re: Gridview Checkbox state being lost
Jan 13, 2009 08:53 PM|LINK
I'm not doing the databinding for the gridview (gridview.databind()) in my programming code, ASP is doing it for me. I presume you're suggesting I need to to it manually in my code...
Thanks for your response.
Kerminator
Member
5 Points
28 Posts
Re: Gridview Checkbox state being lost
Jan 13, 2009 09:20 PM|LINK
Am I wrong in thinking that there HAS to be a Databind when a "Modify" Commandfield is clicked or the subsequent "Save" button is clicked? Wouldn't ASP need to Databind in order to display the just-modified values in the refreshed gridview?
ecbruck
All-Star
98772 Points
9690 Posts
Moderator
MVP
Re: Gridview Checkbox state being lost
Jan 14, 2009 12:05 PM|LINK
After the update has happened, then you SHOULD re-bind your GridView to update the data. However, make sure you DO NOT re-bind your GridView each and every PostBack as this can cause havoc on your state.
Microsoft MVP - ASP.NET
Kerminator
Member
5 Points
28 Posts
Re: Gridview Checkbox state being lost
Jan 14, 2009 01:44 PM|LINK
But the thing is, I did not write any of the update code. The "Modify" link for each row is an ASP.Net CommandField that when clicked, shows a textbox for each modifiable field in that row along with a "Save" and "Cancel" link (all automatic stuff, built into ASP.Net). When "Save" is clicked, ASP.Net somehow does the update via the UpdateCommand in the SQLDataSource that drives the gridview. Clicks on both "Modify" and "Save" cause postbacks to the page. So how would I know if the postback was caused by a click on "Modify" or "Save"?
BTW, many thanks for your replies. There are no other ASP programmers here where I work, so the Internet is my only lifeline...
ecbruck
All-Star
98772 Points
9690 Posts
Moderator
MVP
Re: Gridview Checkbox state being lost
Jan 14, 2009 01:58 PM|LINK
Ok, that makes sense now. When your Update occurs, your SqlDataSource performs the Update, and then again performs your SelectCommand. This, is what is wiping out your CheckBox state. What you will need to do is to save the state of your CheckBoxes within the Session or ViewState. This can be done a variety of ways, but basically everytime a CheckBox is checked, you must save its state somewhere that it is now Checked or Unchecked. Then after every re-bind of your data, you can pull this state out of the Session or ViewState and re-set the state of the CheckBoxes. Here's an example of how I would maintain the state:
ASPX
CODE-BEHIND
Microsoft MVP - ASP.NET
Kerminator
Member
5 Points
28 Posts
Re: Gridview Checkbox state being lost
Jan 14, 2009 03:31 PM|LINK
Thanks Ed. I'm a VB guy so it will take me a little while to digest your C# codebehind, but I'll give your technique a try as soon as I'm able. I appreciate the help!
Kerminator
Member
5 Points
28 Posts
Re: Gridview Checkbox state being lost
Jan 14, 2009 08:08 PM|LINK
Yes Ed, I used a permutation of the code you suggested and it worked fine. Thanks for the idea!