获取CheckBox.Checked在ASPxGridView中没有正确地进行Woking

时间:2017-08-28 10:02:37

标签: asp.net aspxgridview

我在ASPxGridView中有这样的复选框:

<dxwgv:ASPxGridView ID="Grid_Loading_Plan_Detail" runat="server" >          
...
<dxwgv:GridViewDataColumn Caption="#" VisibleIndex="1">
<DataItemTemplate>
<dxe:ASPxCheckBox ID="loadingStatus" runat="server" Checked='<%# GetChecked(Eval("loadingStatus").ToString()) %>'></dxe:ASPxCheckBox>
</DataItemTemplate>
</dxwgv:GridViewDataColumn>
...
</dxwgv:ASPxGridView>

当加载复选框时,它很好地绑定,这里是函数:

Public Function GetChecked(value As String) As Boolean
    Select Case value
        Case "1"
            Return True
        Case "0"
            Return False
        Case Else
            Return False
    End Select
End Function

问题是,当检查checked状态时,它总是返回加载的值。如果第一次加载时状态为checked,然后我执行unchecked CheckBox,则仍会返回checked。这里我如何获取值并将其保存到数据库:

Protected Sub btSimpan_Click(sender As Object, e As EventArgs) Handles btSimpan.Click
    Try
        sqlstring = ""
        For i As Integer = 0 To Grid_Loading_Plan_Detail.VisibleRowCount - 1
            Dim loadingStatus As DevExpress.Web.ASPxEditors.ASPxCheckBox = Grid_Loading_Plan_Detail.FindRowCellTemplateControl(i, Grid_Loading_Plan_Detail.Columns(1), "loadingStatus")

            sqlstring = sqlstring & " UPDATE containerTransaction SET loadingStatus = '" & IIf(loadingStatus.Checked, "1", "0") & "' " & _
                        " WHERE ID = '" & Grid_Loading_Plan_Detail.GetRowValues(i, "ID") & "'; "
        Next

        If SQLExecuteNonQuery(sqlstring) > 0 Then
            Response.Redirect("loading-plan.aspx")
        End If
    Catch ex As Exception
        Response.Write("Error btSimpan_Click <BR> " & ex.ToString)
    End Try
End Sub

ADDED

这是我如何绑定数据:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
        If Not Page.IsPostBack Then
            Call Load_menu()
        End If
    Catch ex As Exception
        Response.Write("Page_Load Exception :<br>" & ex.ToString)
    End Try

    If Not Session("Grid_Loading_Plan_Detail") Is Nothing Then
        Grid_Loading_Plan_Detail.DataSource = CType(Session("Grid_Loading_Plan_Detail"), DataTable)
        Grid_Loading_Plan_Detail.DataBind()
    End If
End Sub

这里是Load_menu()函数:

Private Sub Load_menu()
    Try
        sqlstring = "SELECT ID, code, date, container, seal, sender, receiver, [weight], loadingStatus " & _
            "FROM containerTransaction " & _
            "WHERE loadingCode = (SELECT TOP 1 code FROM loadingPlan WHERE ID = '" & ID & "') AND [status] = 1 " & _
            "ORDER BY [weight] "
        DS = SQLExecuteQuery(sqlstring)
        DT = DS.Tables(0)
        Session("Grid_Loading_Plan_Detail") = DT
        Grid_Loading_Plan_Detail.DataSource = DT
        Grid_Loading_Plan_Detail.KeyFieldName = "ID"
        Grid_Loading_Plan_Detail.DataBind()
    Catch ex As Exception
        Response.Write("Load_Menu Exception :<br>" & ex.ToString)
    End Try
End Sub

我在这里错过了什么?

1 个答案:

答案 0 :(得分:3)

这是会话绑定问题,它检查会话是否可用,然后数据源将与之绑定。代码是:

If Not Session("Grid_Loading_Plan_Detail") Is Nothing Then
    Grid_Loading_Plan_Detail.DataSource = CType(Session("Grid_Loading_Plan_Detail"), DataTable)
    Grid_Loading_Plan_Detail.DataBind()
End If

我删除了该变量,一切正常。

修改

如果删除会话检查程序,则搜索和排序存在问题。我有一个新的解决方案,通过搜索一些更合适的文章和ASPxGridView中的搜索/排序功能仍在工作。

在init中调用load_menu()函数是这种情况的最佳方法:

Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
    Call Load_menu()
End Sub

您可以删除此案例的会话变量。如果此解决方案有问题,请告诉我。