0
votes

I am having an issue with gridview and checkboxes in asp.net. I bind the grid at page load and add Attributes to the checkbox in the BindGrid() function:

chkSelection.Attributes.Add("onclick", "AbortPostBack(); __doPostBack('" + chkSelection.UniqueID + "','');")
AddHandler chkSelection.CheckedChanged, AddressOf ChkSelector_CheckedChanged

On page 1 I select some rows. I move to page 2, select another row. As soon as I do that, the entire grid just disappears!

On debugging, I found that grdSelect.Rows is 0 in the ReadGrid() function below:

in Page_Load

If Not Page.IsPostBack Then
    BuildData()
    BindGrid()
    RenumberPager()
    Dim ctlControl As UserControl = PageUtility.SearchControl(Page.Master, "ctlLeadsCount")
    CType(ctlControl, leadscount).Count = m_SelectedRecords.Count
Else            
    ReadGrid()
End If

in ReadGrid

For Each row As GridViewRow In grdSelect.Rows
    Dim chkSelection As CheckBox = CType(row.FindControl("ChkSelector"), CheckBox)
    If Not chkSelection Is Nothing Then
        Dim recNumK As DataKey = CType(grdSelect.DataKeys(row.RowIndex), DataKey)
        Dim recNum As String = recNumK.Value.ToString()
        m_SelectedRecords.Remove(recNum)

        If chkSelection.Checked = True Then
            For Each rec As Record In m_data
                If rec(BusinessFieldNames.UniqueId) = recNum Then
                    m_SelectedRecords(recNum) = rec(BusinessFieldNames.UniqueId)
                    Exit For
                End If
            Next
        End If
    End If
Next

ViewState("Cherry") = m_SelectedRecords

Any suggestions/pointers would be really appreciated.

1
Are you creating your grid dynamically, or is it declared in the aspx page?Dante
It is declared in the aspx page.pro_karma

1 Answers

0
votes

You have to Re-bind your data in gridView_PageIndexChanging event. Also before binding you have to set the Page index property of your gridview like this:

        Protected Sub grdSelect_PageIndexChanging(...) Handles grdSelect.PageIndexChanging
  grdSelect.PageIndex = e.NewPageIndex
  BuildData()
  BindGrid()
end sub

Hope it helps.