What's Here?
- Members: 85,025
- Replies: 353,880
- Topics: 51,658
- Snippets: 1,758
- Tutorials: 418
- Total Online: 1,261
- Members: 41
- Guests: 1,220
Who's Online?
|
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
|
|
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
//Namespace Reference
using System.Collections;
/// <summary>
/// method to repopulate selected items in the GridView
/// NOTE: This is so if the user pages back to page they've been
/// to the selected items on that page are still selected
/// </summary>
private void RePopulateCheckedItems(GridView grid, string ctrl)
{
ArrayList checkedItems = new ArrayList ();
string chkBoxIndex;
CheckBox chk;
checkedItems = (ArrayList)Session["CheckedItems"];
//make sure we have selected items
if (!(checkedItems == null))
{
//loop through each row in the GridView
foreach (GridViewRow row in grid.Rows)
{
//get the index of each row
chkBoxIndex = (string)grid.DataKeys[row.RowIndex].Value.ToString();
//check to see if that index is in our ArrayList
if (checkedItems.Contains(chkBoxIndex))
{
//find the CheckBox column since it's in the ArrayList
chk = (CheckBox)row.FindControl(ctrl);
//check the CheckBox
chk.Checked = true;
}
//results = (string[])checkedItems.ToArray(typeof(string));
////now create our delimited string
//chkdItems = string.Join(",", results);
}
}
}
Copy & Paste
|
|
|
Searchw00t
Over 265,000 Pages!
Reference Sheets
Bye Bye Ads
Free DIC T-Shirt
Related Sites
Monthly Drawing
Partners
Top Contributors
Top 10 Kudos This Month
|