| |

VerySource

 Forgot password?
 Register
Search
View: 911|Reply: 6

DataGrid implements multiple selection across pages. Where can I save the selected value? ? ?

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-14 11:40:01
| Show all posts |Read mode
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;
}
}
}
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-18 10:45:01
| Show all posts
do someone know that? ? ? Is it necessary to use the paging that comes with the DataGrid (DataGrid1_PageIndexChanged).
Reply

Use magic Report

0

Threads

15

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-22 19:18:01
| Show all posts
override OnPreRender (...) // Place LoadCheckedState here and try.
{
LoadCheckedState ();
}
Reply

Use magic Report

0

Threads

15

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-22 20:00:01
| Show all posts
override OnLoad (...) // Remember all checked keys before binding.
{
SaveCheckedItems ();
base.OnLoad (...);
}

override OnPreRender (...) // Place LoadCheckedState here and try. After binding, restore the check.
{
LoadCheckedState ();
base.OnPreRender (...);
}
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-7-11 17:30:02
| Show all posts
What are the return types of OnLoad(...) and OnPreRender(...) methods? ? ?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-26 21:00:02
| Show all posts
mark
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-26 21:15:01
| Show all posts
Use session
After the operation is over, clear the session
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list