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

Sushant TorankarRank #3,026 · 476 reputationPosted Dec 16, 2024, 11:30 AM EST
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 YadavRank #217 · 11.3K reputationPosted Dec 16, 2024, 7:43 PM EST
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
function storeCheckedItems() { var selectedIds = []; $('input[type="checkbox"]:checked').each(function() { selectedIds.push($(this).closest('tr').find('.ReferenceId').text()); }); $('#').val(selectedIds.join(',')); }hfSelectedIdsvalue and perform the update based on the IDs stored.protected void gvData_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvData.PageIndex = e.NewPageIndex; BindGrid(); } protected void btnApproveSelected_Click(object sender, EventArgs e) { string selectedIds = hfSelectedIds.Value; // Get the comma-separated IDs if (!string.IsNullOrEmpty(selectedIds)) { var idsToApprove = selectedIds.Split(','); // Process the IDs to update the database foreach (var id in idsToApprove) { // Update your records based on the ID } } } protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { var chk = (CheckBox)e.Row.FindControl("chkSelect"); if (chk != null) { // You can keep the checkbox checked or unchecked based on certain conditions } } } private void BindGrid() { // Your code to bind data to GridView }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.
Sangeetha SRank #212 · 11.4K reputationPosted Dec 17, 2024, 2:15 AM EST
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#
using System; using System.Collections.Generic; using System.Web.UI.WebControls; public partial class YourPage : System.Web.UI.Page { private static List checkedIds = new List(); // To store checked IDs protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGrid(); } } private void BindGrid() { // Bind your GridView here, for example: GridView1.DataSource = GetData(); // GetData() should return your data source GridView1.DataBind(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { SaveCheckedItems(); // Save checked IDs before changing page GridView1.PageIndex = e.NewPageIndex; BindGrid(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { int id = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ID")); CheckBox chk = (CheckBox)e.Row.FindControl("CheckBox1"); chk.Checked = checkedIds.Contains(id); // Check if the ID is in the list } } protected void Button1_Click(object sender, EventArgs e) { // Here you can use the checkedIds list to update your records foreach (int id in checkedIds) { // Update your database records as needed } } private void SaveCheckedItems() { checkedIds.Clear(); // Clear the list to repopulate it foreach (GridViewRow row in GridView1.Rows) { CheckBox chk = (CheckBox)row.FindControl("CheckBox1"); int id = Convert.ToInt32(DataBinder.Eval(row.DataItem, "ID")); if (chk != null && chk.Checked) { checkedIds.Add(id); // Add checked ID to the list } } } private List GetData() { // Fetch your data from the database or any other source // Return a list of your data type } private bool IsChecked(object id) { return checkedIds.Contains(Convert.ToInt32(id)); // Check if the ID is checked } }Abhishek YadavRank #217 · 11.3K reputationPosted Dec 16, 2024, 10:24 AM EST
can you please explain me the flow and perticularry what technolagy you are dealing with