Asp.net

在 ASPxGridView 中獲取 CheckBox.Checked 不能正常工作

  • May 30, 2018

我在 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第一次載入然後我執行uncheckedCheckBox,它仍然返回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

添加

這裡我如何綁定數據:

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

我在這裡錯過了什麼?

這是會話綁定問題,它檢查會話是否可用,然後數據源將與之綁定。程式碼是:

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

對於這種情況,您可以刪除會話變數。如果此解決方案有問題,請告訴我。

引用自:https://stackoverflow.com/questions/45916438