Visual Basic ASP.NET未正确读取未选中的复选框

时间:2014-07-24 13:26:11

标签: asp.net vb.net gridview checkbox

我有一个Web应用程序,它从SQL数据库中读取以创建gridview。从这个gridview我想选择一些数据并通过会话变量将其传回另一个页面。我试图通过选中之前选中的框来创建连续性,但是如果我取消选中它们并将信息发回,则未选中的框仍保留在会话变量数据中。

这是检查会话变量并相应地检查框的代码。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim String1 As String = ""
    Dim String2 As String = ""
    Dim boolcheck As Boolean = False
    For Each row As DataRow In employeelist.Rows
        String1 = row.Item(0)
        For Each row2 As GridViewRow In EmployeeListGridView.Rows
            String2 = row2.Cells(1).Text
            If String1 = String2 Then
                Dim checkRow As CheckBox = TryCast(row2.Cells(0).FindControl("checkRow"), CheckBox)
                checkRow.Checked = True
            End If
        Next
    Next
End Sub

这是将数据表存储到会话变量

的代码
Protected Sub SelectButton_Click(sender As Object, e As EventArgs) Handles SelectButton.Click
    Dim dt As New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("ZID"), New DataColumn("Last Name")})
    For Each row As GridViewRow In EmployeeListGridView.Rows
        If row.RowType = DataControlRowType.DataRow Then
            Dim checkRow As CheckBox = TryCast(row.Cells(0).FindControl("checkRow"), CheckBox)
                If checkRow.Checked Then
                Dim zid As String = row.Cells(1).Text
                Dim lastname As String = row.Cells(3).Text
                dt.Rows.Add(zid, lastname)
            End If
        End If
    Next

    Session("employeedt") = dt
    Response.Redirect("ManagerTraining.aspx")
End Sub

如果在第一位代码中检查了一个框,并且用户未选中,则第二段代码将进入"如果checkRow.checked"即使用户取消选中该框,也要声明。复选框包含在gridview中。我正在使用ASP.NET和VB。我觉得如果用户取消选中,则不会提交更改。

1 个答案:

答案 0 :(得分:2)

请阅读ASP.NET lifecycle.

Page.Load总是在回发事件处理程序之前执行。您应该将代码设置为

中的复选框值
If not isPostback Then
   Dim checkRow As CheckBox = TryCast(row2.Cells(0).FindControl("checkRow"), CheckBox)
   checkRow.Checked = True
End If