Maintaining State of CheckBoxes While Paging in a GridView Control
page 1 of 6
Published: 27 Feb 2006
Abstract
In this article, I will demonstrate how you can maintain the state of CheckBoxes while paging in a GridView control.
by Mohammad Azam
Feedback
Average Rating: 
Views (Total / Last 10 Days): 68284/ 881

Creating the GridView Control

Checkbox selection inside the GridView control with no paging can be accomplished by writing a few lines of code. In this article, I will demonstrate how you can maintain the state of the CheckBoxes that are present inside the GridView with paging enabled.

The first thing you need to do is to create the GridView control. This can be done simply by dragging and dropping the GridView from the Toolbox onto the web form. Once you have the GridView, you can edit its columns using the smart tag or the HTML view of the page. I will add two bound columns and one template column. The bound columns "CategoryID" and "CategoryName" will be bound to the database fields "CategoryID" and "CategoryName" respectively. The template column will be used to display the CheckBoxes in the GridView control.

After adding the columns, the html source of the GridView control looks like this.

Listing 1: GridView HTML Code

<asp:GridView ID="GridView1" runat="server" 
AutoGenerateColumns="False" AllowPaging="True"  
PageSize="5" Width="324px" DataKeyNames="CategoryID" 
OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Please also note that the paging is enabled in the GridView control, and the DataKeyNames property has been set to "CategoryID," since it’s the primary key in the database. The PageSize property has been set to 5, which means that after one page of the GridView can only contain 5 records/rows. The ItemTemplate tag contains the CheckBox whose ID is "CheckBox1," which means that CheckBoxes will be displayed for the last column.


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 1 and 5 and type the answer here:

User Comments

Title: GridView1_PageIndexChanging()   
Name: Ramakrishna
Date: 12/4/2008 4:56:17 AM
Comment:
RememberOldValues(); is calling repeatedly in
GridView1_PageIndexChanging()please help me.
Title: selectall rows across pagination   
Name: micky
Date: 11/25/2008 10:49:41 PM
Comment:
hello,

Can anyone tell me how to set all pages selected ,when selectall link in headerrow is checked.its urgent.
Title: Specified cast is not valid.   
Name: Sunil
Date: 11/24/2008 4:54:09 AM
Comment:
I m getting error :Specified cast is not valid.

@ index = (int)GridView1.DataKeys[row.RowIndex].Value;

????????????????? using vs 2008
Title: using combobox   
Name: manojmeeurt
Date: 11/15/2008 12:01:41 AM
Comment:
hi,plz help me .
suppose i have one combobox .and two checkbox and i want to del,update,save,by the help of this combobox.and checkbox work for this when i will click chek all then all value are checked and when i click to none checkbox then uncheck all the record...and the work of combobox when i click the combox the box show to me update the record,del,save...plz help me my id is

manojmeerut@rocketmail.com
Title: Using Textbos   
Name: Rachana
Date: 10/31/2008 3:40:47 AM
Comment:
Hi,This article is really good and it worked perfectly with the checkbox.I have the textbox also in the grid along with the checkbox.How to maintain the status of the Textbox along with the checkbox...Please help and reply to this mail id--rachanatj2004@yahoo.co.in

Regards
Rachna
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: satyanarayana
Date: 10/29/2008 9:14:29 AM
Comment:
Hi guy,
Really it's excellent and everything is working fine.
Thanks for u r great work.

Regards
D.Satyanarayana
satyadnet@gmail.com
Title: Issue   
Name: Man
Date: 10/27/2008 1:46:43 AM
Comment:
Comment:
I tried to implement your code in of my webpages
But its popping an error saying

The name 'CHECKED_ITEMS' Does not exist
How can i get rid of this one
Please Let me know ?
Title: CheckBox status after Postack in GridView   
Name: Harish KV
Date: 9/29/2008 5:20:41 AM
Comment:
I was simply Impressed...

Thanks
Title: I need the same code for TextBox   
Name: Suresh
Date: 9/12/2008 8:42:02 AM
Comment:
Hello!! Any one plz help me with the same sort of code for the TextBox. Fighting like anything for past two days.
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: Carlos Cuapio
Date: 9/10/2008 10:12:51 AM
Comment:
Excellent! Thanks for the article. you are great

Greetings from Mexico
Title: nice coding   
Name: Shiva
Date: 8/25/2008 1:13:43 AM
Comment:
Cod is working Fine thanks...keep it up:)
Title: For those who love to code in VB   
Name: Fred
Date: 7/21/2008 1:38:11 PM
Comment:
Instead of using <> operator in the if condition (IN VB)for comparing the arraylist

use If not arraylist is nothing Then

This solved my problem
Title: Issue   
Name: Fred
Date: 7/17/2008 4:15:19 PM
Comment:
I tried to implement your code in of my webpages
But its popping an error saying

The name 'CHECKED_ITEMS' Does not exist
How can i get rid of this one
Please Let me know ?
Title: Question...................   
Name: Lyka
Date: 6/27/2008 4:29:59 AM
Comment:
Error3. The name 'CHECKED_ITEMS' does not exist in the current context. What does this mean?...
Title: Thanks   
Name: Gabriel
Date: 6/12/2008 11:28:41 AM
Comment:
Excelente. It works very well. thanks

Gabriel From Argentina
Title: the solution   
Name: dan
Date: 6/2/2008 9:09:04 AM
Comment:
this solution of using session is very bad because if there are alot of records inside the gridvie the server will be busy

the best way to do this solution is with HiddenField
Title: Index was out of range. Must be non-negative and less than the size of the collection   
Name: GreggSie
Date: 5/23/2008 8:06:43 AM
Comment:
There needs to be values in DataKeyNames property
Title: Index was out of range. Must be non-negative and less than the size of the collection   
Name: geeram
Date: 4/23/2008 10:36:07 PM
Comment:
getting error : Index was out of range. Must be non-negative and less than the size of the collection.
in line no.

index = (int)gdview1.DataKeys[row.RowIndex].Value;

i changed this line to
Index = Convert.ToInt32 (gdview1.DataKeys [row.RowIndex]. Values;

still i am getting the same error.

can anyone plz help
Title: I need save the CategoryName into array.   
Name: Rodrigo
Date: 4/18/2008 4:48:54 PM
Comment:
Hello, Thanks for your article, I need save the categoryname into array, how do I do that?
Title: It is working now   
Name: sameer ali khan
Date: 4/16/2008 6:46:08 AM
Comment:
Hi,
I had just posted the query to you.Please ignore that.I was able to rectify my problem.Now it is working.Thankyou.
Title: Cannot mantain state of checkboxes while using custom paging   
Name: sameer ali khan
Date: 4/16/2008 6:32:45 AM
Comment:
Hi,
I am using custom paging in gridview and i took help from your example.It is working fine in gridview inbuilt paging but when custom paging, i cannot mantain state of page. Can you please give me any idea regarding it?
Title: a little fix   
Name: Maralbust
Date: 4/8/2008 10:21:54 AM
Comment:
Hello everybody I download the code, but I got the same mistake that other ones, to fix the code to do the following:

Index = (int) GridView1.DataKeys [row.RowIndex]. Value;
For
Index = Convert.ToInt32 (GridView1.DataKeys [row.RowIndex]. Value);

In both functions and RePopulateValues and rememberoldvalues and that is, the code running :) I hope will help them.

Greetings from Chile
Title: error : Index was out of range. Must be non-negative and less than the size of the collection.   
Name: Ram
Date: 3/12/2008 3:40:56 AM
Comment:
getting error : Index was out of range. Must be non-negative and less than the size of the collection.
in line no.

index = (int)gdviewPolicyDetails.DataKeys[row.RowIndex].Value;
Title: error : Index was out of range. Must be non-negative and less than the size of the collection.   
Name: salon
Date: 3/11/2008 4:53:15 AM
Comment:
getting error : Index was out of range. Must be non-negative and less than the size of the collection.
in line no.

index = (int)gdviewPolicyDetails.DataKeys[row.RowIndex].Value;
I have changed as
index = Convert.ToInt32(gdviewPolicyDetails.DataKeys[row.RowIndex].Value);
but still getting the same error
Title: Checkbox Problem   
Name: Poonam
Date: 3/3/2008 2:38:09 AM
Comment:
For Each row As GridViewRow In GridView2.Rows
Dim ch As New System.Web.UI.WebControls.CheckBox
ch = (row.FindControl("CheckBox1"))
If (ch.Checked = True) Then
Dim s As String = GridView2.SelectedRow.Cells(1).ToString()
End If
Next

''I am getting value false eventhough checkbox is checked
i am not able to understand why can u plz mail me at
poonamwalimbe@yahoo.co.in
Title: testing for > 0   
Name: Ben
Date: 1/2/2008 9:57:23 AM
Comment:
I don't understand why you are testing for categoryIDList.Count > 0 before updating Session[CHECKED_ITEMS]... this seems to be a bug.

Consider what happens if Session[CHECKED_ITEMS] currently contains a categoryIDList containing a few items. Then the user goes and unchecks everything, so all items will be removed from categoryIDList and categoryIDList.Count will end up being 0. As a result, Session[CHECKED_ITEMS] will not be updated, and the previously selected items will remain stored in Session[CHECKED_ITEMS].
Title: checkbox   
Name: amarnath reddy
Date: 12/28/2007 6:07:56 AM
Comment:
This is very good article.
Title: remember Textbox   
Name: Helen
Date: 11/28/2007 2:05:30 PM
Comment:
This is an excellent article. Thank you. Now I'm trying to do the same thing but with textbox values. i'm unable to make it work.. could anyone show sample code to help me out?
Title: checkbox   
Name: mobina
Date: 11/28/2007 7:36:55 AM
Comment:
i prepared a grid view for displaying my contents of table. I had considered for all the rows a check box..with out using the edit template ..if i need need to delete a particular row that in had checked recently..how can i go with
Title: Check   
Name: saiprasad.teegala@valuelabs,net
Date: 9/19/2007 5:54:16 AM
Comment:
hi sriLakshmi ,in page load
write if(!IsPostBack)
{
write the entire logic here

}
Title: My version with VStudio 2005   
Name: Daniel Perez
Date: 9/13/2007 11:57:51 AM
Comment:
Can you mail me? I have other version that works,
because your version did not work for me.
I tried to post it here, but I got and error.
bye,
dany7487@yahoo.com
Title: What if I am creating the grid dynamically   
Name: Syed Anas Razvi
Date: 8/31/2007 6:01:21 AM
Comment:
Hello All,
I am OK with the code... The effort is appreciable..
My scenario is like this...
I am creating my grid at runtime and upon Clicking of a button, I wanted RememberOldValues() to be called before postback happens because of the button click... Where should I call the RememberOldValues().
Title: need the equivalent code for datagrid   
Name: winnie
Date: 8/30/2007 1:14:06 AM
Comment:
hi
i need the equivalent code for datagrid

index = (int) GridView1.DataKeys[row.RowIndex].Value;
Title: Doesn't get the true value for checked CheckBox   
Name: Srilakshmi
Date: 8/11/2007 5:55:12 AM
Comment:
bool result = ((CheckBox)row.FindControl("CheckBox1")).Checked;
im getting "false" value for even checked CheckBox, can't find anything wrong. Please help!
please send to srilakshmi15@gmail.com

Note: There's no column for checkbox in the database, i want to use checkbox to select a value.
Title: Code in VB.NET (2/2)   
Name: Canellaf
Date: 7/17/2007 11:36:30 AM
Comment:
Protected Sub gv_cont_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv_cont.DataBound
RePopulateValues()
End Sub

Protected Sub gv_cont_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv_cont.PageIndexChanging
RememberOldValues()
gv_cont.PageIndex = e.NewPageIndex
gv_cont.DataBind()
End Sub
Title: Code in VB.NET   
Name: Canellaf
Date: 7/17/2007 11:35:44 AM
Comment:
Thanks a lot.

Working with VB.NET (I've also changed some names for my specific project)

Private Sub RememberOldValues()

Dim index As String
Dim result As Boolean
Dim prs_login_List As New ArrayList()
Dim gvRow As GridViewRow
Dim CheckBox As System.Web.UI.WebControls.CheckBox

For Each gvRow In gv_cont.Rows

index = gv_cont.DataKeys(gvRow.RowIndex).Value
CheckBox = gvRow.FindControl("CheckBox1")
result = CheckBox.Checked

' Check in the Session
If Not IsNothing(Session("CHECKED_ITEMS")) Then
prs_login_List = Session("CHECKED_ITEMS")
End If

If result Then
If Not prs_login_List.Contains(index) Then
prs_login_List.Add(index)

End If

Else
prs_login_List.Remove(index)

End If

Next
If Not IsNothing(prs_login_List) Then
If prs_login_List.Count > 0 Then
Session("CHECKED_ITEMS") = prs_login_List
End If
End If
End Sub

Private Sub RePopulateValues()

Dim index As String
Dim prs_login_List As New ArrayList()
Dim gvRow As GridViewRow
Dim CheckBox As System.Web.UI.WebControls.CheckBox
prs_login_List = Session("CHECKED_ITEMS")

If Not IsNothing(prs_login_List) Then
If prs_login_List.Count > 0 Then

For Each gvRow In gv_cont.Rows
index = gv_cont.DataKeys(gvRow.RowIndex).Value

If prs_login_List.Contains(index) Then

CheckBox = gvRow.FindControl("CheckBox1")
CheckBox.Checked = True
End If
Next
End If
End If

End Sub

Protected Sub gv_cont_DataBound(ByVal sender As Object, ByVal e
Title: re   
Name: raj
Date: 7/4/2007 1:48:00 AM
Comment:
very very gud
Title: hi..............   
Name: lopa
Date: 6/4/2007 7:18:20 AM
Comment:
foreach (GridViewRow row in GridView1.Rows).
im workin in vb.net replacing of this code line is " For Each row As GridViewRow In DG_Persons.rows". im gettin error line in DG_persons.rows.
plz.........seggest me
Title: hi...can i have this code in vb.net   
Name: lopa
Date: 6/4/2007 3:24:24 AM
Comment:
hi..........i want to do same job in ASP.net with vb language. If yes...plz pescribe me, how to do?b'cz i m tryin in vb. bt it's nt worked out.

Thank you

Waitin,
lopashree@rediffmail.com
Title: hai it is very useful   
Name: senthil kumar
Date: 6/2/2007 6:32:22 AM
Comment:
Hai
it is very useful
Title: Keys is not int   
Name: Akram
Date: 6/1/2007 10:48:45 PM
Comment:
Hi
What if the data key was not int (e.g. string), or more than 1 column?!!
how to do it then?
Thanks
Title: Error   
Name: Manju
Date: 5/17/2007 2:55:27 AM
Comment:
error : Index was out of range. Must be non-negative and less than the size of the collection.

private void rememberoldvalues()
{
ArrayList emplist = new ArrayList();
int index = -1;

foreach (GridViewRow row in GridView1.Rows)
{

index = (int)GridView1.DataKeys[2].Value;
bool result = ((CheckBox)row.FindControl("CheckBox1")).Checked;

// Check in the Session
if (Session[CHECKED_ITEMS] != null)
emplist = (ArrayList)Session[CHECKED_ITEMS];
if (result)
{
if (!emplist.Contains(index))
emplist.Add(index);
}
else
emplist.Remove(index);
}
if (emplist != null && emplist.Count > 0)
Session[CHECKED_ITEMS] = emplist;


}
Title: Fix for the repopulation problem   
Name: Udit
Date: 5/15/2007 11:11:56 AM
Comment:
The reason that the re-population of the checkboxes doesn't work is, as mentioned in a post below, that the DataBind runs AFTER the PageIndexChanged has run.

The solution to this is to simply call the RePopulateValues() in the OnDataBound event, and it work perfectly.
Title: Using RadioButtonList   
Name: Javed Bux
Date: 2/21/2007 2:04:19 AM
Comment:
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;

Label lbl = (Label)row.FindControl("Label1");
//int index = Convert.ToInt32(lbl.Text);

if (categoryIDList.Contains(index))
{
RadioButtonList RD = (RadioButtonList)row.FindControl("RadioButtonList1");
RD.Items[index].Selected = true; // error in this line


//CheckBox myCheckBox = (CheckBox)row.FindControl("CheckBox1");
// myCheckBox.Checked = true;
}
}

}

}
Title: Repopulating RadioButtonList in GridView   
Name: Javed Bux
Date: 2/21/2007 2:02:31 AM
Comment:
Thanks for this Article... it is very usefull for checkbox but when i am using RadioButtonList it is not working as on third page to first page. I am using this for online exam RadioButtonList is use for choice of answer i.e A ,B ,C and D.

private void RememberOldValues()
{
ArrayList categoryIDList = new ArrayList();
int index = -1;
foreach (GridViewRow row in GridView1.Rows)
{


Label lbl = (Label)row.FindControl("Label1");
//index = Convert.ToInt32(lbl.Text);

// bool result = ((RadioButtonList)row.FindControl("RadioButtonList1")).SelectedValue;

RadioButtonList RD = (RadioButtonList)row.FindControl("RadioButtonList1");

// Check in the Session
if (Session["CHECKED_ITEMS"] != null)
categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];

for (int i = 0; i < RD.Items.Count; i++)
{
if (RD.Items[i].Selected == true)
{
index = (int)GridView1.DataKeys[row.RowIndex].Value;
// string rem = RD.Items[i].ToString();

// string Qu = "Insert Into AnswerBank(UserName,QuestionID,SelectedAns) values ('Javed','" + ID + "','" + rem + "') ";
//dataAccess.SaveData(Qu);

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
Title: It is nice but....   
Name: Naresh Gohil
Date: 2/13/2007 11:43:41 AM
Comment:
Hi,
I read this article and find the solution of my problem
But if i don't want to use primary key value than what's the solution.

... And I want to ask onr more question related to sorting

How to sort all the records in the gridview in paging
Title: Great Ariticle   
Name: Rajendran Thiagarajan
Date: 2/12/2007 12:03:00 PM
Comment:
Mohammad,

Thanks for your article. It's simple and great!!

-Rajendran Thiagarajan.
.Net Developer
Title: Session["CHECKED_ITEMS"] Error   
Name: Amol
Date: 2/6/2007 11:39:58 PM
Comment:
I'm getting this error.Can you help me with this
The name 'CHECKED_ITEMS' does not exist in the current context

Thanks
Title: Anonymous is right!   
Name: JonB
Date: 12/15/2006 5:58:09 PM
Comment:
Hey, thanks Azam.. before I read this article, I implemented this in a very similar fashion, except I add/remove to the Arraylist as I actually check the checkboxes (using postback for the actual checkbox. I had such a headache using the OnPageIndexChanged event, and after reading Anonymous's post, I moved the "repopulatecheck" method call into a DataBound even method and it worked. In fact, in my implementation, I don't even use the OnPageIndexChanged event at all! Still, thanks for creating this, it re-affirmed my implementation, and also lead to finding a solution. Thanks Azam, and Anonymous :)
Title: How did you fix it   
Name: Joe
Date: 12/7/2006 2:46:04 PM
Comment:
How did you fix the following problem? I am having the same issue.

Title: applying using datagrid, VB.NEt
Name: maya
Date: 12/1/2006 2:36:27 AM
Comment:
im using datagrid, VB.NEt, this should be work, but it cannot, it gave me error :-
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
maybe u could fix it :P .. :(
Title: Venkat   
Name: Venkat
Date: 12/5/2006 5:08:49 AM
Comment:
This is very helpful for me.
Title: Thanks Maya   
Name: AzamSharp
Date: 12/1/2006 10:34:42 PM
Comment:
Hi Maya,

I am glad that you found it useful.

:)
Enjoy the weekened!
Title: TQ   
Name: maya
Date: 12/1/2006 10:18:17 PM
Comment:
i've finally fixed it.. thanx u!! really.. this is an awesome article.. b4 this yur article popup sending values also save my day... so this is the 2nd time uu help me.. yeayyyyyyyyy
Title: applying using datagrid, VB.NEt   
Name: maya
Date: 12/1/2006 2:36:27 AM
Comment:
im using datagrid, VB.NEt, this should be work, but it cannot, it gave me error :-
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
maybe u could fix it :P .. :(

Private Sub RememberOldValues()
Dim categoryIDList As ArrayList = New ArrayList
Dim index As Integer = -1

For Each row As DataGridItem In dgList.Items
index = CType(dgList.DataKeys(row.ItemIndex).Value, Integer)

Dim result As Boolean = CType(row.FindControl("chkSelect"), CheckBox).Checked

If Not (Session.Item("CHECKED_ITEMS") Is Nothing) Then
categoryIDList = CType(Session("CHECKED_ITEMS"), ArrayList)
End If

If result Then
If Not categoryIDList.Contains(index) Then
categoryIDList.Add(index)
End If
Else
categoryIDList.Remove(index)
End If
Next

If Not (categoryIDList Is Nothing) AndAlso categoryIDList.Count > 0 Then
Session.Item("CHECKED_ITEMS") = categoryIDList
End If

End Sub

Private Sub RePopulateValues()
Dim categoryIDList As ArrayList = CType(Session.Item("CHECKED_ITEMS"), ArrayList)
If Not (categoryIDList Is Nothing) AndAlso categoryIDList.Count > 0 Then
For Each row As DataGridItem In dgList.Items
Dim index As Integer = CType(dgList.DataKeys(row.ItemIndex).Value, Integer)
If categoryIDList.Contains(index) Then
Dim myCheckBox As CheckBox = CType(row.FindControl("chkSelect"), CheckBox)
myCheckBox.Checked = True
End If
Next
End If
End Sub
Title: gridview and datagrid   
Name: maya
Date: 12/1/2006 1:08:26 AM
Comment:
hello azam,

i've tried applying this code to datagrid.. but the code behind gave syntax error on "rows is not a member of datagrid"... btw,what's the difference?
Title: RE: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: AzamSharp
Date: 11/23/2006 11:43:11 AM
Comment:
Download the sample and you will find out.
Title: RE: a little question!   
Name: AzamSharp
Date: 11/23/2006 11:41:44 AM
Comment:
Hi xiaofen_zh2003@163.com,

You need to check that the DataKeys contains the required datatype which is int.

You can try something like this:

int index = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);

Hope it helps!
Title: Maintaining state of checkboxes   
Name: Abongile
Date: 11/23/2006 4:05:24 AM
Comment:
This is a great example, I've just used it and it worked the first time
Title: Repopulating checkboxes in GridView   
Name: Ali
Date: 11/23/2006 2:18:28 AM
Comment:
Reply to Liza:
problem is not with Azam's code,its just perfect.
what seems 2 me is you have set checkbox's autopostback property to true which causes da problem. As if Asp.net checkbox's autoppostback property is set to true, then when its contianer control's databind is called, checkbox's checkedchanged event is fired, which is i duno abnormal behavior or wat eva. how to fix it probably Azam bhai will tell us..;) pleez bro..!
Title: a little question!   
Name: xiaofen_zh2003@163.com
Date: 11/22/2006 11:45:24 PM
Comment:
Hello Mr. Azam,first thank you very much for your help about The GridView Control.but i have a question about using it. when the program run on
"index = (int)GridView1.DataKeys[row.RowIndex].Value", it happened with "System.InvalidCastException: convert is invalid" how to resolve it?
xiaofen_zh2003@163.com. thank you very much!
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: KamalRaturi
Date: 11/20/2006 5:32:25 PM
Comment:
The Article is very helpful.
It is easy to understand and Implement.

Thanks!
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: Ahad
Date: 11/20/2006 7:11:03 AM
Comment:
Great Artical, very easy to follow:)
Title: Doesn't get the true value for checked CheckBox   
Name: kashif
Date: 11/18/2006 12:12:43 AM
Comment:
bool result = ((CheckBox)row.FindControl("CheckBox1")).Checked;
im getting "false" value for even checked CheckBox, can't find anything wrong. Please help!

Note: There's no column for checkbox in the database, i want to use checkbox to select a value.
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: ArabASP
Date: 11/15/2006 2:45:04 PM
Comment:
This line could cause me error : Session[CHECKED_ITEMS]
Can you tell us where to define this?
Title: Realy A very very good piece of work.   
Name: Soma
Date: 11/10/2006 4:19:35 AM
Comment:
It realy help me a lot. Thank u very much.........
Title: Creating The GridView Control   
Name: Jayakumar
Date: 11/9/2006 4:27:59 AM
Comment:
Thanks for the article. It reduces my time. :)
Title: Good piece of code   
Name: RED
Date: 11/7/2006 1:59:51 PM
Comment:
This is a good example on how to treat checkboxes inside the GridView Component. Thanx
Title: Thanks   
Name: AzamSharp
Date: 11/7/2006 11:29:31 AM
Comment:
I am glad that it helped you!
Title: Creating The GridView Control   
Name: Cuong Nguyen
Date: 11/2/2006 2:37:51 AM
Comment:
Thanks a lot
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: krishna som
Date: 10/11/2006 2:31:55 AM
Comment:
it helped me lot ... thanks
Title: CheckBox with GridView   
Name: Greivin Batz
Date: 9/28/2006 11:12:01 AM
Comment:
Thank you your help.... very helpful !!!
Title: Work with a textbox   
Name: scott
Date: 8/30/2006 11:42:01 AM
Comment:
Hey gridviewguy, can this example also work for remembering what was entered into a textbox? If so how?
Title: Re Repopulating checkboxes in GridView solution   
Name: Asif
Date: 8/28/2006 8:53:12 AM
Comment:
hi anonymus,

thanks for the solution but can you elaborate the code a bit for a clear understanding of every one
Title: Thanks   
Name: Azamsharp
Date: 8/13/2006 8:44:00 PM
Comment:
Thanks!
Title: Repopulating checkboxes in GridView solution   
Name: Anonymous
Date: 8/13/2006 8:30:45 PM
Comment:
This is a great solution! However, when I first tested I get the same problem as everyone where the checkboxes get reset after every page changed. In order to solve this problem, you need to add DataBound event to repopulate the checkboxes states. Below is the solution:

protected void GridView1_DataBound(Object sender, EventArgs e)
{
RePopulateValues();
}

protected void GridView1_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
RememberOldValues();
}

Note: This is done in .Net 2.0 and the reason the author code doesn't work is because the databound event is called after pageindexchanging and pageindexchanged and therefore it clears the checkboxes states.
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: Thendral
Date: 7/20/2006 6:42:21 AM
Comment:
i used same code as in site but i erroe stating that ..
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


any ideas...
Title: PreRender?   
Name: Collin
Date: 7/10/2006 3:41:39 PM
Comment:
Since my checkbox is clearly getting checked in the RePopulate function, it be getting cleared somewhere later in the event model. I used Trace output to check the value of myCheckbox.Checked at various points in the page life cycle and found that it was being reset to false at PreRenderComplete. It is true during PreRender and then false during PreRenderComplete.

Why is this happening? Shouldn't the values of controls be fixed by the time we get to PreRenderComplete? The .NET Framework Class Library says this about PreRenderComplete:

"The PreRenderComplete event is raised when the pre-render stage of the page life cycle is complete. At this stage of the page life cycle, all controls are created, any pagination required is completed, and the page is ready to render to the output."

It mentions pagination but doesn't say exactly what occurs regarding this.

Now, if I move the RePopulate function to PreRenderComplete, the checkboxes get populated correctly and it seems to work. But this is a significant deviation from the author's solution. Does anybody know what's happening here?
Title: Same Problems   
Name: Collin
Date: 7/9/2006 9:56:02 PM
Comment:
I am having the same problems that others mentioned. I am adding the correct items to the Session object, but when I set 'myCheckBox.Checked = true' in RePopulateValues, nothing happens to the checkboxes. Pretty much exactly as Seth described.

Liza, I'm not sure if I understand your last comment. Are you saying the method of data binding is the problem? That is, manually binding the data instead of using a DataSourceId and GridView.DataBind? I am also using DataSourceId. There's no reason I can see why that shouldn't work. I can try to manually bind the data as Mohammad does and see what happens.

If anybody has any more insight into this problem it would be much appreciated (especially the author!)

Thanks.
Title: Reply To Seth   
Name: Liza
Date: 7/6/2006 1:15:44 PM
Comment:
Hi Seth,
I wanted you to know that I used the exact example as Mohammed gave and the checkboxes are now maintained. I was using a defined DataSourceID so that I could cut down the database based on a parameter. My previous not-working example contained a where clause in the datasource ID. I had to select ALL the columns in the database, but at least I got the example to work. Now I'll work on incorporating my WHERE clause and I'll let you know how it goes.
Title: Problems   
Name: Seth
Date: 6/28/2006 9:16:01 PM
Comment:
What is happening to Liza is also happening to me; I have figured out that the new page's checkboxes are not being displayed and instead the current page is being checked during repopulate... not sure why, but perhaps theres a different place to call repopulate than in the PageIndexChanging method, as for us it does not actually update the new page when we change the page index, but does so after that method call completes.
Title: Maintaining state of checkboxes   
Name: Moupiya
Date: 6/15/2006 1:27:22 AM
Comment:
Hi!!!
I used your code.The values are coming in the function but finally when the page loads the check boxes are not checked.
I am using GridView DataBind() instead of BindData function.Moreover i have used HtmlCheckBoxes.
Please help out..
moupiyadas@yahoo.com
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: Swap
Date: 5/4/2006 3:05:04 AM
Comment:
Thanks a lot.
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: alyque
Date: 3/23/2006 8:36:52 PM
Comment:
Very helpful... Thanks!
Title: Maintaining State of CheckBoxes While Paging in a GridView Control   
Name: Lola
Date: 3/22/2006 9:15:29 AM
Comment:
Good job on this article
Title: Repopulating checkboxes in GridView   
Name: Liza Pulsifer
Date: 3/7/2006 9:32:18 AM
Comment:
Hello Mr. Azam,
I used your code snippets in an effort to repopulate the checkboxes in a GridView. The code appears to check the appropriate boxes. I set a breakpoint at the and verified variables at the line "myCheckBox.Checked = true;" However, when the page finally appears, the checkboxes are empty. I use "grdGridView.DataBind();" instead of your "BindData()"
subroutine. Any ideas why the checkboxes are not appearing?
Title: Creating The GridView Control   
Name: bjack8468@satx.rr.com
Date: 3/6/2006 11:48:40 AM
Comment:
I finally got this to work so you can ignore the error message that I sent yesterday. I have one other question, do you know how to write this in VB for ASP; I would be most greatful. Good job on this article.

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 12/4/2008 3:07:20 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search