GridView中的复选框问题

时间:2012-02-25 20:47:12

标签: c# asp.net vb.net gridview

我在asp.net中遇到gridview和复选框的问题。我在页面加载时绑定网格,并将属性添加到BindGrid()函数中的复选框:

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

在第1页上,我选择了一些行。我转到第2页,选择另一行。一旦我这样做,整个网格就会消失!

在调试时,我发现下面的ReadGrid()函数中grdSelect.Rows为0:

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

任何建议/指示都会非常感激。

1 个答案:

答案 0 :(得分:0)

您必须在gridView_PageIndexChanging事件中重新绑定数据。在绑定之前,你必须像这样设置gridview的Page index属性:

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

希望它有所帮助。