Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
165
Unbound checkbox losing state after second postback
posted

Hi,

We have found the following behavior with Web Grid, and want to confirm if the same is a bug.

Created an Unbound column of CheckBox Type, and placed a 'Test Postback' on the page (outside the WebGrid).

  1. Check one or more Check Boxes

  2. Click 'Test Postback'

    1. It works fine for the first postback.

    2. User sees Check Boxes selected still.

  3. Click 'Test Postback' again

    1. This empties unchecks all Check Boxes in the Column.

Can somebody tell me if this behavior is by design or is a bug?

Also, is there any solution as we need to keep the CheckBox selection as is across all postbacks (Except converting the unbound column to a bound column and preserving its state in source data object).

 

Regards,

 

MPVBRao.

Parents
No Data
Reply
  • 45
    posted

    Hi you can resolve this problem with the property ViewState of .NetFrameWork, you can create a datatable and Save in the ViewState Property

    First initialize the datatable only in the first postback of ther form:

    Dim dt As New DataTable()

    dt.Columns.Add(New DataColumn(<KeyOfDatasource>, GetType(Decimal)))

    dt.Columns.Add(New DataColumn(<state of checkbox>, GetType(Boolean)))
    For Each row As GridRecord In webdatagrid.Rows
    Dim dr As DataRow = Nothing
    dr = dt.NewRow()
    dr("IdFact") = If(IsDBNull(row.Items(1).Value), "", Trim(row.Items(1).Value))
    dr("Estado") = False
    dt.Rows.Add(dr)
    Next
    'Store the DataTable in ViewState
    ViewState("CurrentTable") = dt

    then you can add a client event in the aspx form:

    function webdatagrid_Editing_CellValueChanged(sender, eventArgs) {
    ///<summary>
    ///
    ///</summary>
    ///<param name="sender" type="Infragistics.Web.UI.WebDataGrid"></param>
    ///<param name="eventArgs" type="Infragistics.Web.UI.CellValueChangedEventArgs"></param>

    if (eventArgs.get_cell().get_column().get_key() == "UnboundCheckBoxField_0") {
    var callbackObj = sender._callbackManager.createCallbackObject();
    sender._callbackManager.execute(callbackObj);
    }

    }// -->

    then in ther server side create a rowupdated event and then you save the new status of the checkbox edited by the user:

    Protected Sub webdatagrid_RowUpdated(sender As Object, e As RowUpdatedEventArgs) Handles webdatagrid.RowUpdated
    Dim dt As DataTable
    dt = DirectCast(ViewState("CurrentTable"), DataTable)
    For Each row As GridRecord In wdgFacturas.Rows
    dt.Rows(row.Index)(<State of Checkbox>) = row.Items(0).Value 'cell with the value edited by the user
    Next
    ViewState("CurrentTable") = dt

    End Sub

    Then you need create other event for example RowSelectionChanged

    Protected Sub webdatagrid_RowSelectionChanged(sender As Object, e As SelectedRowEventArgs) Handles webdatagrid.RowSelectionChanged

    Dim dt As DataTable
    dt = DirectCast(ViewState("CurrentTable"), DataTable)
    For Each row As GridRecord In wdgFacturas.Rows
    dt.Rows(row.Index)(<state of checkbox>) = row.Items(0).Value
    Next
    ViewState("CurrentTable") = dt

    sbSeteaEstadoCheckBox() ' HERE CALL A METOD FOR INITIALIZATE THE CHECKBOX ROWS IN THE DATAGRID

    end sub

    AND CREATE THE METOD TO INITIALIZE THE VALUES 

    Private Sub sbSeteaEstadoCheckBox()
    Try
    Dim dt As DataTable
    dt = DirectCast(ViewState("CurrentTable"), DataTable)
    Dim nose As Integer = 0
    For Each row As GridRecord In webdatagrid.Rows
    row.Items(0).Value = dt.Rows(row.Index)(<state of checkbox>)
    nose += 1
    Next
    Dim testing = 0
    Catch ex As Exception

    End Try
    End Sub

    With this you save in a view state datatable the selections of the user and then you pass this values on every row selection change

Children
No Data