Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 149,029 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,847 people online right now. Registration is fast and FREE... Join Now!





Get checked item in gridView (ASP.Net) and store in session

This is a snippet I use to get the checked items in an ASP.Net GridView and store them in the session for later retrieval. I use this so I don't lose track of the checked items during paging

Submitted By: PsychoCoder
Actions:
Rating:
Views: 8,760

Language: C#

Last Modified: March 25, 2008
Instructions: Need a reference to the System.Collections Namespace. Pass the method the name of your GridView, and the name of your CheckBox column in your GridView.

Snippet


  1. //Namespaces needed
  2. using System.Collections;
  3.  
  4.  
  5. /// <summary>
  6. /// method to retrieve all the selected items in the GridView
  7. /// NOTE: This is so we dont lose track of them while paging
  8. /// </summary>
  9. private void GetCheckedItems(string crtl,GridView grdView)
  10. {
  11.     //instantiate new ArrayList to hold our checked items
  12.     ArrayList checkedItems = new ArrayList();
  13.     CheckBox chk;
  14.     string chkBoxIndex = string.Empty;
  15.     //loop through each row in the GridView
  16.     foreach (GridViewRow row in grdView.Rows)
  17.     {
  18.         //get the index of the current CheckBox
  19.         chkBoxIndex = (string)grdView.DataKeys[row.RowIndex].Value.ToString();
  20.         chk = (CheckBox)row.FindControl(ctrl);
  21.         //add ArrayList to Session is if doesnt already exist
  22.         if (!(Session["CheckedItems"] == null))
  23.         {
  24.             checkedItems = (ArrayList)Session["CheckedItems"];
  25.         }
  26.  
  27.         //now see if the current CheckBox is checked
  28.         if (chk.Checked)
  29.         {
  30.             //see if the current value is in the Session, if not add it
  31.             if (!(checkedItems.Contains(chkBoxIndex)))
  32.             {
  33.                 //add to the list
  34.                 checkedItems.Add(chkBoxIndex);
  35.             }
  36.             else
  37.             {
  38.                 //remove from list since it's unchecked
  39.                 checkedItems.Remove(chkBoxIndex);
  40.             }
  41.         }
  42.  
  43.     }
  44.     //update Session with the list of checked items
  45.     Session["CheckedItems"] = checkedItems;
  46. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month