|
I use stored procedures for pagination, and I want to use ViewState to save the checkbox checkbox value in the DataGrid custom template column, but I didn't think of where to save the checkbox checkbox value. After selecting "Next Page" or "Previous Page", Page_Load displays data from another page of the DataGrid. It has been submitted before I selected the checkbox. How can I get the value? (Don't say Mencius's example, the paging that comes with the DataGrid that he uses has the DataGrid1_PageIndexChanged event, which I don't have here)
There are several well-written methods below, but I don't know where to call the SaveCheckedItems () method. Page_Load doesn't seem to be big, which is why I didn't think of it. . . .
----------------------------------
// Get (create) a collection that saves the selected state
ArrayList CheckedItemCollection
{
get
{
if (ViewState ["CheckedItems"] == null)
ViewState ["CheckedItems"] = new ArrayList ();
return ViewState ["CheckedItems"] as ArrayList;
}
}
// Add item
private void AddCheckedItem (int key)
{
ArrayList al = CheckedItemCollection;
if (! al.Contains (key))
al.Add (key);
}
// remove item
private void RemoveCheckedItem (int key)
{
ArrayList al = CheckedItemCollection;
if (al.Contains (key))
al.Remove (key);
}
/ * Each time you turn the page, you need to save the check state of the page.
* Operation logic:
* For the selected item, store the ID in the state collection
* For unselected items, if the item ID exists in the collection, delete it.
* /
private void SaveCheckedItems ()
{
for (int i = 0; i <DataGrid1.Items.Count; i ++)
{
CheckBox cb = DataGrid1.Items [i] .Cells [0] .FindControl ("CheckBox1") as CheckBox;
if (cb! = null)
{
int key = Convert.ToInt32 (DataGrid1.Items [i] .Cells [1] .Text);
if (cb.Checked)
this.AddCheckedItem (key);
else
this.RemoveCheckedItem (key);
}
}
}
/ * After each DataGrid binding,
* This method is required to load the tick status from the status set.
* /
void LoadCheckedState ()
{
ArrayList al = CheckedItemCollection;
for (int i = 0; i <DataGrid1.Items.Count; i ++)
{
CheckBox cb = DataGrid1.Items [i] .Cells [0] .FindControl ("CheckBox1") as CheckBox;
if (cb! = null)
{
int key = Convert.ToInt32 (DataGrid1.Items [i] .Cells [1] .Text);
if (al.Contains (key))
cb.Checked = true;
}
}
} |
|