Code Snippets

  

C# Source Code


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

Join 85,025 C# Programmers. There are 1,261 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!


Recheck CheckBoxes in ASP.Net GridView

This is a snippet (used in conjunction with the snippet found at http://www.dreamincode.net/code/snippet1858.htm) that will take your selected items in your GridView (which was stored in an ArrayList then applied to a Session value) and reselect them if the user pages back to that page

Submitted By: PsychoCoder
Actions:
Rating:
Views: 817

Language: C#

Last Modified: March 25, 2008
Instructions: Requires a reference to the System.Collections Namespace.

Provide the method with the name of your GridView and the name of your CheckBox Column.

This snippet will take the value created in the snippet located at http://www.dreamincode.net/code/snippet1858.htm and repopulate an ArrayList with those selected values. It will then loop through your GridView, index by index, checking the values in the ArrayList against the index of the GridView. If the row was selected it will recheck the CheckBox for that row.

Snippet


  1. //Namespace Reference
  2. using System.Collections;
  3.  
  4.  
  5. /// <summary>
  6. /// method to repopulate selected items in the GridView
  7. /// NOTE: This is so if the user pages back to page they've been
  8. /// to the selected items on that page are still selected
  9. /// </summary>
  10. private void RePopulateCheckedItems(GridView grid, string ctrl)
  11. {
  12.     ArrayList checkedItems = new ArrayList();
  13.     string chkBoxIndex;
  14.     CheckBox chk;
  15.  
  16.     checkedItems = (ArrayList)Session["CheckedItems"];
  17.  
  18.     //make sure we have selected items
  19.     if (!(checkedItems == null))
  20.     {
  21.         //loop through each row in the GridView
  22.         foreach (GridViewRow row in grid.Rows)
  23.         {
  24.             //get the index of each row
  25.             chkBoxIndex = (string)grid.DataKeys[row.RowIndex].Value.ToString();
  26.             //check to see if that index is in our ArrayList
  27.             if (checkedItems.Contains(chkBoxIndex))
  28.             {
  29.                 //find the CheckBox column since it's in the ArrayList
  30.                 chk = (CheckBox)row.FindControl(ctrl);
  31.                 //check the CheckBox
  32.                 chk.Checked = true;
  33.             }
  34.             //results = (string[])checkedItems.ToArray(typeof(string));
  35.             ////now create our delimited string   
  36.             //chkdItems = string.Join(",", results);
  37.         }
  38.     }
  39. }

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