Hello,
I am binding Gridview with suppose 1000 records. I need to add checkbox infront of each row and user will check any checkbox they want and There should be common APPROVE button. On clicking on it, it should update status of CHECKED rows only
But on Pagination it's not working properly. Suppose If i check one checkbox on 1st Page and one checkbox on 2nd page, it should give Id for that 2 checked checkbox, but instead it gives me extra checkbox checked on 2nd page, so total it gives me 3 checkbox checked.
so how to handle it properly ?
| Choose | Reference Id | Status | Action |
|---|---|---|---|
| checkbox1 | 12345 | Y | APPROVE |
| checkbox2 | 78945 | Y | APPROVE |
| checkbox3 | 74185 | Y | APPROVE |
| checkbox4 | 85296 | Y | APPROVE |
APPROVE
Sangeetha SPosted Dec 17, 2024, 7:15 AM
To achieve the functionality of persisting the state of checkboxes across paginated pages in a GridView in ASP.NET, you can follow these general steps:
Steps to Implement:
Maintain State: Use a data structure to maintain the state of the checkboxes (e.g., a
Listto store the IDs of checked records).Handle GridView Events: Manage the
RowDataBoundandPageIndexChangingevents of the GridView to update the state as users interact with the checkboxes.Store Checked States: When the user checks or unchecks a checkbox, update the underlying data structure to reflect the current state.
Rebind the GridView: Ensure the GridView is rebound on postbacks to reflect the correct state of the checkboxes
C#
Abhishek YadavPosted Dec 17, 2024, 12:43 AM
In your scenario where you're using a GridView with pagination and checkboxes, the issue arises because when you switch between pages, the state of the checkboxes (which rows are checked) is not maintained. By default, ASP.NET GridView's checkboxes get reset after a postback, especially during pagination, because the GridView is re-rendered and the control state is lost.
To resolve this, you need to preserve the state of the checkboxes across page changes. Here's how you can approach this issue:
Steps to handle checkbox states on pagination:
Use Client-Side (JavaScript) or Hidden Fields to track the checked checkboxes. The idea is to store the IDs of the selected rows either in JavaScript or a hidden field on the server-side to maintain their state between postbacks and pagination.
Handle the checkbox state on server-side: After the user clicks the "APPROVE" button, you can use the IDs stored in the hidden field or JavaScript variable to identify the selected checkboxes, and then perform the necessary update.
Below is an example using hidden fields to store the selected IDs across page changes.
Solution Using Hidden Field
hfSelectedIdsvalue and perform the update based on the IDs stored.Explanation:
hfSelectedIds) is used to store the IDs of the rows that are selected.storeCheckedItems) is used to populate the hidden field with the selected IDs whenever a user selects or deselects a checkbox.This way, the selected checkboxes will persist across pagination, and the selected rows' IDs can be easily accessed for updating the status when the "APPROVE" button is clicked.
Sushant TorankarPosted Dec 16, 2024, 4:30 PM
Hello Abhishek,
Flow is : I want to update status of checked checkbox in gridview. On pagination of grid, it should persist the state of checkbox, if it checked or not.
So for ex. : If I check one checkbox on 1st page and one checkbox on 2nd page, I should be able to get ID of that checked record and so I can update respcted record in table
I am looking a solution in asp.net
Abhishek YadavPosted Dec 16, 2024, 3:24 PM
can you please explain me the flow and perticularry what technolagy you are dealing with