Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 85,026 C# Programmers. There are 1,262 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C# Expert
Powered by LivePerson.com

Register to Make This Box Go Away!


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: 1,191

Language: C#

Last Modified: December 31, 1969
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.





Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month