Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Detecting changes in children
Message
From
30/11/2004 12:51:15
Max Fillmore
Essential Skills, Inc.
Lenexa, Kansas, United States
 
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
00951764
Message ID:
00965775
Views:
18
Kevin,

Your VB transaltion has a few bugs. I translated it and made a few changes. Here is the corrected version:
        Public Overrides Sub NavigateData(ByVal navPosition As mmNavigate, ByVal raiseEvents As Boolean)
            If raiseEvents AndAlso (navPosition <> mmNavigate.Refresh) Then
                If Not (Me.ActiveControl Is Nothing) Then
                    ' Make sure the current UI control value is written back to the data

                    If TypeOf Me.ActiveControl.Parent Is mmDataGrid Then
                        Dim grid As mmDataGrid = CType(Me.ActiveControl.Parent, mmDataGrid)

                        grid.EndEdit(Nothing, grid.CurrentRowIndex, False)

                        If TypeOf grid.DataSource Is DataSet Then
                            Dim ds As DataSet = CType(grid.DataSource, DataSet)
                            ' Determine the table name
                            Dim TableName As String = Nothing
                            Dim ViewName As String = Nothing

                            Dim BizObj As mmBusinessObject = CType(mmAppDesktop.FormMgr.GetControlBizObj(grid), mmBusinessObject)
                            If Not (BizObj Is Nothing) Then
                                mmBindingStrategyBase.GetBindingSource(BizObj, grid.BindingSourceMember, TableName, ViewName)
                            End If
                            If Not mmString.Empty(TableName) Then
                                ds.Tables(TableName).Rows(grid.CurrentRowIndex).EndEdit()
                            Else
                                ds.Tables(0).Rows(grid.CurrentRowIndex).EndEdit()
                            End If
                        Else
                            If TypeOf grid.DataSource Is DataView Then
                                Dim dv As DataView = CType(grid.DataSource, DataView)
                                dv.Table.Rows(grid.CurrentRowIndex).EndEdit()
                            End If
                        End If
                    Else
                        Dim cntrl As Control = CType(Me.ActiveControl, Control)
                        If cntrl.DataBindings.Count > 0 Then
                            Me.BindingContext(cntrl.DataBindings(0).DataSource).EndCurrentEdit()
                        End If
                    End If
                End If

                Dim IgnoreIsChanged As Boolean = Me.IgnoreIsChanged

                ' We only want to ignore the first time here after IgnoreIsChanged is set.
                If Me.IgnoreIsChanged Then
                    Me.IgnoreIsChanged = False
                End If

                ' Check for changes
                If Me.IsChanged() Then
                    ' This is where we override the default behavior.
                    If Not IgnoreIsChanged Then
                        Dim CancelControl As Control = Me.FocusOnCancelControl
                        Dim SaveControl As Control = Me.FocusOnSaveControl
                        Me.FocusOnCancelControl = Nothing
                        Me.FocusOnSaveControl = Nothing

                        ' Ask the user if they want to save changes
                        Dim result As DialogResult = Me.AskSaveChanges()

                        Me.FocusOnCancelControl = CancelControl
                        Me.FocusOnSaveControl = SaveControl

                        If result = DialogResult.Cancel Then
                            Return
                        End If
                    End If
                End If
            End If

            MyBase.NavigateData(navPosition, raiseEvents)
        End Sub
I found that it is still necessary to provide a way to override inappropriate calls to AskSaveChanges(). I did this by adding a protected variable to ABusinessForm in my BusinessForm.vb:
        ' Ignore IsChanged in NavigateData().
        ' This allows us to not be prompted with AskSaveChanges in NavigateData
        ' when the user clicks instances of btnNew, btnDelete, or btnClose.
        Protected IgnoreIsChanged As Boolean = False
In my forms which are subclassed from ABusinessForm I set IgnoreIsChanged = True in the click method of buttons which call HookPreNavigate() where I am being prompted inappropriately, in the HookPreCancel(), and in HookPreDelete(). For instance, on a given form btnNew is an instance of mmButtonNew. Clicking this button adds a row to the primary bizobj of the form. Since my business objects have
Me.AutoIncrementCustom = True
and
Me.RetrieveAutoIncrementPK = True
the primary key column of the new row is populated with a value of -1 before control comes to my click method where I invoke Me.NavigateData(mmNavigate.Last, True) to synchonize the navigation control grid with the dataset. This causes IsChanged() to be true at the time that NavigateData is executing and this causes an inappropriate call to AskSaveChanges(). I block this with a kluge by setting Me.IgnoreIsChanged = True before the call to NavigateData() like this:
        Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
            If Me.btnNew.Result Then
                ' Ignore the IsChanged property in ABusinessForm.NavigateData
                Me.IgnoreIsChanged = True

                ' Navigate to the new record.  There should be a way around this.
                Me.NavigateData(mmNavigate.Last, True)
            End If
        End Sub
Wouldn't it be better if NavigateData(), rather than calling IsChanged() to determine if the business object has any changes, would instead call something to determine if the current row has any changes? If the current row has changes and the user declines to either save or abandon changes then navigation would be disallowed. I believe this would have the desired result. And would be cleaner than my current work around.

There is also probably a better way even than that. What do you think Kevin?

Max...
Previous
Reply
Map
View

Click here to load this message in the networking platform