Click here to Skip to main content
15,885,665 members
Articles / Web Development / ASP.NET
Article

Persisting checked state across pages in a DataGrid

Rate me:
Please Sign up or sign in to vote.
4.21/5 (13 votes)
4 Sep 20052 min read 75.6K   715   25   8
This is an article about maintaining checked state across pages in a DataGrid.

Introduction

Some time back, I had written an article on how to add check boxes in a grid to select records. Now I am back with some enhancements on that to persist the checked state between paging. Those who are directly reading this article, I would advise them to read the above mentioned article first and come back.

Problem statement

Our problem can be stated as some thing like below:-

A user checks one record in a page and goes to the next page and selects some records there, when he returns to the previous page we need to keep the checked state of whatever records he had checked earlier. I will call it as checked state maintenance across pages. When he is finished checking through the pages, he needs a list of records he has checked.

Our Way

How I achieved the functionality is by keeping the ID of the checked records in a hidden ListBox. On the ItemDataBound event of the grid I check the existence of the ID of the record in the ListBox and set the checked state of the record.

Step 1 Saving the checked records

On the Page_Load, I am calling a private function MaintainState().

I am iterate through all the DataGrid items and check if the checkbox is checked or not. If it is checked I will add it to the ListBox provided it is not there. If the check box is not checked and it is in the ListBox I will remove it.

Here is the implementation of the function:

C#
private void MaintainState()
{
 //state maintaining 
 foreach( DataGridItem di in myDataGrid.Items )
 {
  HtmlInputCheckBox chkBx = (HtmlInputCheckBox)di.FindControl("EmpId") ;
  //if check box is checked add it 
  if( chkBx !=null && chkBx.Checked )
  {
   Label lbl = (Label)di.FindControl("Id");
   ListItem item=new ListItem(lbl.Text ,lbl.Text );
   if(!SelectedList.Items.Contains(item))
   {
    SelectedList.Items.Add(item );  
   }
  }
  //if check box is not checked then 
  if(( chkBx !=null )&& !(chkBx.Checked) )
  {
   Label lbl = (Label)di.FindControl("Id");
   ListItem item=new ListItem(lbl.Text ,lbl.Text );
   //if un checked item is in the list then remove 
   if(SelectedList.Items.Contains(item))
   {
    SelectedList.Items.Remove(item );
   }    
  }
 }
 //end of state maintaining 
}

Step 2 Setting checked state on returning back

Now that we have the checked records in the hidden ListBox, we will have to set the checked state of the check boxes when the data is bound again across pages. We will write this on the ItemDataBound event of the grid, i.e., when data is being bound to the grid. We will check each item when it is bound. If it is an Item or an AlternatingItem then get the ID of the record, check if the ID is in the ListBox and set the checked state.

The implementation is given below:

C#
private void myDataGrid_ItemDataBound(object sender, 
               System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 //check and uncheck based on the item entry in the List 
 if((e.Item.ItemType== ListItemType.Item )||
    (e.Item.ItemType== ListItemType.AlternatingItem ))
 {
  HtmlInputCheckBox chkBx = 
    (HtmlInputCheckBox)e.Item.Cells[0].FindControl("EmpId");
  Label lbl = (Label)e.Item.Cells[1].FindControl("Id"); 
  ListItem item = new ListItem(lbl.Text , lbl.Text ); 
  if(SelectedList.Items.Contains (item))
   chkBx.Checked =true;
  else 
   chkBx.Checked=false; 
 }
}

One thing you have to keep in mind is to clear the ListBox when you are done with ;-).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
India India
Just another Developer
http://anzboy.wordpress.com/

Comments and Discussions

 
GeneralYou can save the index of the selected item Pin
azamsharp25-Aug-07 2:54
azamsharp25-Aug-07 2:54 
Generalits showing error for visual studio 2005 Pin
manju.cdtech16-May-07 21:35
manju.cdtech16-May-07 21:35 
Questionhow to delete them in paging retaining state Pin
great gs13-Apr-07 1:54
great gs13-Apr-07 1:54 
Questionhow to delete these records by single click in different pages Pin
great gs12-Apr-07 23:03
great gs12-Apr-07 23:03 
GeneralWell Done Pin
BennyHarper0122-Mar-07 22:30
BennyHarper0122-Mar-07 22:30 
GeneralVB Version Pin
dclinton28-Nov-05 8:13
dclinton28-Nov-05 8:13 
GeneralRe: VB Version Pin
Owen Gunter12-Dec-06 22:23
Owen Gunter12-Dec-06 22:23 
GeneralViewstate Pin
Ashley van Gerven21-Sep-05 19:09
Ashley van Gerven21-Sep-05 19:09 
You're using a hidden ListBox to maintain data between postbacks. Even though this works, it seems like a bit of a "hack" since that's not the purpose of the ListBox. Do you know that you can use the Viewstate to store custom data?

For example:
ArrayList lst = new ArrayList();
Viewstate.Add("checked_indexes", lst);

Then on postback:
ArrayList lst = (ArrayList)Viewstate["checked_indexes"];

Hope this helps...

Thanks for the article - I hadn't considered that problem with paging / checkboxes.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.