Results 1 to 4 of 4

Thread: Maintain Grid View Checkbox while paging

  1. #1
    cthang is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0 (VS 2005/2008)
    Join Date
    Feb 2009
    Posts
    8
    Reputation
    0

    Maintain Grid View Checkbox while paging

    Hi,

    I am using Visual studios 2008 with VB as the code behind. I have checkboxes in my gridview and also paging. When I check a few items and then go to the next page and come back the checkboxes are unchecked.

    Is there anyway to keep the checkboxes checked while you area paging through the gridview?

    Thanks

  2. #2
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,206
    Reputation
    571
    GridView Checkbox maintaining state - ASP.NET Forums

    VB.NET solution on last post in thread saving to Session.

  3. #3
    cthang is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0 (VS 2005/2008)
    Join Date
    Feb 2009
    Posts
    8
    Reputation
    0
    thanks I will take a look.

  4. #4
    cthang is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.0 (VS 2005/2008)
    Join Date
    Feb 2009
    Posts
    8
    Reputation
    0
    Here is the code I have so far. The problem I am having is the Protected Sub GridView1_PageIndexChanging.

    Private Sub RememberOldValues()
    Dim categoryIDList As New ArrayList()
    Dim index As Integer = -1
    For Each row As GridViewRow In GridView1.Rows
    index = CInt(GridView1.DataKeys(row.RowIndex).Value)
    Dim result As Boolean = DirectCast(row.FindControl("CheckBox1"), CheckBox).Checked

    ' Check in the Session
    If Session("CHECKED_ITEMS") IsNot Nothing Then
    categoryIDList = DirectCast(Session("CHECKED_ITEMS"), ArrayList)
    End If
    If result Then
    If Not categoryIDList.Contains(index) Then
    categoryIDList.Add(index)
    End If
    Else
    categoryIDList.Remove(index)
    End If
    Next
    If categoryIDList IsNot Nothing AndAlso categoryIDList.Count > 0 Then
    Session("CHECKED_ITEMS") = categoryIDList
    End If
    End Sub

    Private Sub RePopulateValues()

    Dim categoryIDList As ArrayList = DirectCast(Session("CHECKED_ITEMS"), ArrayList)
    If categoryIDList IsNot Nothing AndAlso categoryIDList.Count > 0 Then
    For Each row As GridViewRow In GridView1.Rows
    Dim index As Integer = CInt(GridView1.DataKeys(row.RowIndex).Value)
    If categoryIDList.Contains(index) Then
    Dim myCheckBox As CheckBox = DirectCast(row.FindControl("CheckBox1"), CheckBox)
    myCheckBox.Checked = True
    End If
    Next
    End If
    End Sub

    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging

    RememberOldValues()
    GridView1.PageIndex = e.NewPageIndex
    DataBind()
    RePopulateValues()

    End Sub

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking