在ASP.Net GridView控件中分页时保留复选框的状态

时间:2016-11-07 07:54:54

标签: asp.net vb.net gridview

我使用gridview从sql server数据库中分页数据,如果我更改了gridview页面,我检查的复选框在检查后不会保持状态。这是我的代码:

    Dim strSQL As String = ""
    Dim cls As New clsMain
    Dim ds As New DataSet

    strSQL = "SELECT * from tbldata"
    ds = cls.ReturnDataSet(strSQL)



    gvTF.AllowPaging = True
    gvTF.DataSource() = ds
    gvTF.DataBind()

    Dim CheckBoxArray As ArrayList
    If ViewState("CheckBoxArray") IsNot Nothing Then
        CheckBoxArray = TryCast(ViewState("CheckBoxArray"), ArrayList)
    Else
        CheckBoxArray = New ArrayList()
    End If

    If IsPostBack Then
        Dim CheckBoxIndex As Integer
        Dim CheckAllWasChecked As Boolean = False
        Dim chkAll As CheckBox = TryCast(gvTF.HeaderRow.Cells(0).FindControl("checkAll"), CheckBox)

        Dim checkAllIndex As String = "checkAll-" & gvTF.PageIndex

        If chkAll.Checked Then
            If CheckBoxArray.IndexOf(checkAllIndex) = -1 Then
                CheckBoxArray.Add(checkAllIndex)
            End If
        Else
            If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
                CheckBoxArray.Remove(checkAllIndex)
                CheckAllWasChecked = True
            End If
        End If
        For i As Integer = 0 To gvTF.Rows.Count - 1
            If gvTF.Rows(i).RowType = DataControlRowType.DataRow Then
                Dim chk As CheckBox = _
                 DirectCast(gvTF.Rows(i).Cells(0) _
                 .FindControl("chkRow"), CheckBox)
                CheckBoxIndex = gvTF.PageSize * gvTF.PageIndex + (i + 1)
                If chk.Checked Then
                    If CheckBoxArray.IndexOf(CheckBoxIndex) = -1 And _
                        Not CheckAllWasChecked Then
                        CheckBoxArray.Add(CheckBoxIndex)
                    End If
                Else
                    If CheckBoxArray.IndexOf(CheckBoxIndex) <> -1 Or _
                        CheckAllWasChecked Then
                        CheckBoxArray.Remove(CheckBoxIndex)
                    End If
                End If
            End If
        Next
    End If

    ViewState("CheckBoxArray") = CheckBoxArray

以及我在更改页面时的代码:

   Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
    gvTF.PageIndex = e.NewPageIndex
    gvTF.DataBind()
    If ViewState("CheckBoxArray") IsNot Nothing Then
        Dim CheckBoxArray As ArrayList = _
        DirectCast(ViewState("CheckBoxArray"), ArrayList)
        Dim checkAllIndex As String = "checkAll-" & gvTF.PageIndex

        If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
            Dim chkAll As CheckBox = _
            DirectCast(gvTF.HeaderRow.Cells(0) _
            .FindControl("checkAll"), CheckBox)
            chkAll.Checked = True
        End If
        For i As Integer = 0 To gvTF.Rows.Count - 1
            If gvTF.Rows(i).RowType = DataControlRowType.DataRow Then
                If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
                    Dim chk As CheckBox = _
                    DirectCast(gvTF.Rows(i).Cells(0) _
                    .FindControl("chkRow"), CheckBox)
                    chk.Checked = True
                    gvTF.Rows(i).Attributes.Add("style", "background-color:aqua")
                Else
                    Dim CheckBoxIndex As Integer = gvTF.PageSize * (gvTF.PageIndex) + (i + 1)
                    If CheckBoxArray.IndexOf(CheckBoxIndex) <> -1 Then
                        Dim chk As CheckBox = _
                        DirectCast(gvTF.Rows(i).Cells(0) _
                        .FindControl("chkRow"), CheckBox)
                        chk.Checked = True
                        gvTF.Rows(i).Attributes.Add("style", "background-color:aqua")
                    End If
                End If
            End If
        Next
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

分页的GridView仅将当前页面的数据呈现给标记,因此,当尝试遍历其行时,它将仅返回显示在当前分页数据上的可用行。 但是,您可以使用其他方法来解决此问题,Like Bind GridView with Paging using jQuery 要么 Enable Sorting And Paging Callbacks