Get CheckBox.Checked in ASPxGridView Not Woking Properly
Asked Answered
E

1

9

I have checkbox in ASPxGridView like this :

<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>

It bind nicely when the checkbox loaded, here the function :

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

The problem is, when check the checked status, it always return the loaded value. If the status is checked when loaded first time and then I do unchecked the CheckBox, it still return checked. Here How I get the value and save it to database :

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

Here how I bind the data :

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

And here the Load_menu() function :

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

What have I missed here?

Elery answered 28/8, 2017 at 10:2 Comment(6)
How and when are you binding data to the GridView? If you did not wrap the databinding inside an IsPostBack check, the state of the checkboxes will be overwritten by their database values every time a PostBack occurs.Fountainhead
You are also calling your checked function with the loadingStatus as the parameter so whenever it gets checked or unchecked it calls the loading function with the loading paramBengali
Not really sure that I fully understand the case but I think it might be an issue of LoadPostData() missing in your code. Try to call DirectCast(btSimpan, IPostBackDataHandler).LoadPostData(btSimpan.UniqueID, Request.Form) right after this line Dim loadingStatus As DevExpress.Web.ASPxEditors.ASPxCheckBox =.... and see. btw I'm not really into VB.NET so I converted my C# snippet to this piece. The original in C# is ((IPostBackDataHandler)btSimpan).LoadPostData(btSimpan.UniqueID, Request.Form);. Hope it helps.. If so please tell me and I will post a full answer tomorrow. Good luck :)Olodort
@Fountainhead : I added the function in question, Thank you.Elery
@Bengali : Do you have any other solution to bind it from database? Thank youElery
@JaqenH'ghar : Thank you for your help but I have try your code and still have same behaviour :(Elery
E
3

It was session binding problem, it checked if session available, then data source will bind with that. The code is :

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

I removed that variable, and everything was ok.

Edit

It have problem for searching and sorting if I removed the session checker. I have new solution from searching through some articles which fit better and searching/sorting feature in ASPxGridView still working.

Calling load_menu() function in init is the best way for this case :

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

You can remove the session variable for this case. If there is problem with this solution, let me know.

Elery answered 2/9, 2017 at 4:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.