locked
Retain Gridview's Previous Page Values RRS feed

  • Question

  • User-717444423 posted

    Good Evening..

    I have a gridview with a checkbox button column(itemtemplate). This column will be used to select multiple rows in the grid.

    My problem is everytime go to another page, the checked items in the previous page are gone and the items are all unchecked again.

    Is there a way to retain the checkbox check values before I update the database? So that everytime I go from one page (gridview) to another I can see what rows I have already checked?

    If it is not possible, what is the best work around?

    Thanks.

    Tuesday, July 1, 2008 10:14 AM

Answers

  • User-1995538749 posted

    Here's an example:

    ASPX 

    <%@ page language="C#" masterpagefile="~/MasterPages/Default.master" autoeventwireup="true"
    	codefile="MaintainCheckBoxStateWhenPaging.aspx.cs" inherits="GridView_MaintainCheckBoxStateWhenPaging"
    	title="GridView: Maintain CheckBox State When Paging" %>
    
    <asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="Server">
    	<asp:linkbutton id="btnSelectAll" runat="server" commandname="SelectAll" 
    		text="Select All" oncommand="SelectDeselect" />
    		|
    	<asp:linkbutton id="btnDeselectAll" runat="server" commandname="DeselectAll" text="Deselect All"
    		oncommand="SelectDeselect" />
    	<asp:gridview id="gvProducts" runat="server" allowpaging="True" autogeneratecolumns="False"
    		datakeynames="ProductID" datasourceid="ldsProducts" style="margin-top: 20px;"
    		onpageindexchanging="gvProducts_PageIndexChanging" onrowdatabound="gvProducts_RowDataBound">
    		<columns>
    			<asp:templatefield headertext="Select">
    				<itemtemplate>
    					<asp:checkbox id="chkSelect" runat="server" />
    				</itemtemplate>
    			</asp:templatefield>
    			<asp:boundfield datafield="ProductID" headertext="ProductID" insertvisible="False"
    				readonly="True" sortexpression="ProductID" />
    			<asp:boundfield datafield="ProductName" headertext="ProductName" sortexpression="ProductName" />
    		</columns>
    	</asp:gridview>
    	<asp:linqdatasource id="ldsProducts" runat="server" contexttypename="LinqToSql.NorthwindDataContext"
    		tablename="Products">
    	</asp:linqdatasource>
    </asp:content>

    CODE-BEHIND 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.UI.WebControls;
    
    public partial class GridView_MaintainCheckBoxStateWhenPaging : System.Web.UI.Page
    {
    	private List<int> ProductIDs
    	{
    		get
    		{
    			if (this.ViewState["ProductIDs"] == null)
    			{
    				this.ViewState["ProductIDs"] = new List<int>();
    			}
    
    			return this.ViewState["ProductIDs"] as List<int>;
    		}
    	}
    
    	protected void SelectDeselect(object sender, CommandEventArgs e)
    	{
    		foreach (GridViewRow gvr in gvProducts.Rows)
    		{
    			CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
    
    			if (chkSelect != null)
    			{
    				chkSelect.Checked = e.CommandName.Equals("SelectAll");
    			}
    		}
    	}
    
    	protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    	{
    		foreach (GridViewRow gvr in gvProducts.Rows)
    		{
    			CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
    
    			if (chkSelect != null)
    			{
    				int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex]["ProductID"]);
    
    				if (chkSelect.Checked && !this.ProductIDs.Contains(productID))
    				{
    					this.ProductIDs.Add(productID);
    				}
    				else if (!chkSelect.Checked && this.ProductIDs.Contains(productID))
    				{
    					this.ProductIDs.Remove(productID);
    				}
    			}
    		}
    	}
    
    	protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
    	{
    		GridViewRow gvr = e.Row;
    
    		if (gvr.RowType == DataControlRowType.DataRow)
    		{
    			CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
    
    			if (chkSelect != null)
    			{
    				int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex]["ProductID"]);
    
    				chkSelect.Checked = this.ProductIDs.Contains(productID);
    			}
    		}
    	}
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 1, 2008 10:22 AM

All replies

  • User-1995538749 posted

    Here's an example:

    ASPX 

    <%@ page language="C#" masterpagefile="~/MasterPages/Default.master" autoeventwireup="true"
    	codefile="MaintainCheckBoxStateWhenPaging.aspx.cs" inherits="GridView_MaintainCheckBoxStateWhenPaging"
    	title="GridView: Maintain CheckBox State When Paging" %>
    
    <asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="Server">
    	<asp:linkbutton id="btnSelectAll" runat="server" commandname="SelectAll" 
    		text="Select All" oncommand="SelectDeselect" />
    		|
    	<asp:linkbutton id="btnDeselectAll" runat="server" commandname="DeselectAll" text="Deselect All"
    		oncommand="SelectDeselect" />
    	<asp:gridview id="gvProducts" runat="server" allowpaging="True" autogeneratecolumns="False"
    		datakeynames="ProductID" datasourceid="ldsProducts" style="margin-top: 20px;"
    		onpageindexchanging="gvProducts_PageIndexChanging" onrowdatabound="gvProducts_RowDataBound">
    		<columns>
    			<asp:templatefield headertext="Select">
    				<itemtemplate>
    					<asp:checkbox id="chkSelect" runat="server" />
    				</itemtemplate>
    			</asp:templatefield>
    			<asp:boundfield datafield="ProductID" headertext="ProductID" insertvisible="False"
    				readonly="True" sortexpression="ProductID" />
    			<asp:boundfield datafield="ProductName" headertext="ProductName" sortexpression="ProductName" />
    		</columns>
    	</asp:gridview>
    	<asp:linqdatasource id="ldsProducts" runat="server" contexttypename="LinqToSql.NorthwindDataContext"
    		tablename="Products">
    	</asp:linqdatasource>
    </asp:content>

    CODE-BEHIND 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.UI.WebControls;
    
    public partial class GridView_MaintainCheckBoxStateWhenPaging : System.Web.UI.Page
    {
    	private List<int> ProductIDs
    	{
    		get
    		{
    			if (this.ViewState["ProductIDs"] == null)
    			{
    				this.ViewState["ProductIDs"] = new List<int>();
    			}
    
    			return this.ViewState["ProductIDs"] as List<int>;
    		}
    	}
    
    	protected void SelectDeselect(object sender, CommandEventArgs e)
    	{
    		foreach (GridViewRow gvr in gvProducts.Rows)
    		{
    			CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
    
    			if (chkSelect != null)
    			{
    				chkSelect.Checked = e.CommandName.Equals("SelectAll");
    			}
    		}
    	}
    
    	protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    	{
    		foreach (GridViewRow gvr in gvProducts.Rows)
    		{
    			CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
    
    			if (chkSelect != null)
    			{
    				int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex]["ProductID"]);
    
    				if (chkSelect.Checked && !this.ProductIDs.Contains(productID))
    				{
    					this.ProductIDs.Add(productID);
    				}
    				else if (!chkSelect.Checked && this.ProductIDs.Contains(productID))
    				{
    					this.ProductIDs.Remove(productID);
    				}
    			}
    		}
    	}
    
    	protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
    	{
    		GridViewRow gvr = e.Row;
    
    		if (gvr.RowType == DataControlRowType.DataRow)
    		{
    			CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
    
    			if (chkSelect != null)
    			{
    				int productID = Convert.ToInt32(gvProducts.DataKeys[gvr.RowIndex]["ProductID"]);
    
    				chkSelect.Checked = this.ProductIDs.Contains(productID);
    			}
    		}
    	}
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 1, 2008 10:22 AM
  • User-717444423 posted

    I tried  your code with a new project and it works. Just one thing, I'm still not familiar with Linq so what I did is to use the sqldatasource object as the datasource and your code stil worked.

    I'm used in using the code behind in filling up my gridview's datasource using dataset/datatables on page load, instead of using data objects. And that's where my problem started. Everytime the page loads, my datasource is also refreshed. Now I know, I'm used with the wrong practice. But its ok.. now I know.

    Thanks for your help. I'll start from there. [:D]

     

    Tuesday, July 1, 2008 11:27 PM