【问题标题】:Visual Basic ASP.NET Not Reading Unchecked Checkbox ProperlyVisual Basic ASP.NET 未正确读取未选中的复选框
【发布时间】:2014-07-24 21:38:16
【问题描述】:

我有一个从 SQL 数据库读取数据以创建网格视图的 Web 应用程序。从这个 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

如果在页面加载时从第一个代码位中选中了一个框,而用户未选中,则即使用户未选中该框,第二段代码也会进入“If checkRow.checked”语句。复选框包含在网格视图中。我正在使用 ASP.NET 和 VB。如果用户取消选中,我觉得没有提交更改。

【问题讨论】:

    标签: asp.net vb.net gridview checkbox


    【解决方案1】:

    请阅读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
    

    【讨论】:

    • 感谢您的明确回答和额外的阅读材料。非常感谢。
    • 没问题。我知道这是一个让每个人都不知所措的问题
    猜你喜欢
    • 2016-03-06
    • 2017-11-17
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    相关资源
    最近更新 更多