Button events not being raised after PropertyHasChanged

Button events not being raised after PropertyHasChanged

Old forum URL: forums.lhotka.net/forums/t/2155.aspx


C_J posted on Wednesday, January 17, 2007

I am using CLSA 2.0.3 WinForm 2.0.

I have a Save button on a form that is wired to a saveButton_Click event. This event gets periodically raised after an object property has changed. e.g If I update the FirstName textbox in the UI then click the Save button the objects FirstName property setter gets executed and raises the PropertyHasChanged method. However, the button click event will sometimes get raised and won't other times (I cannot find a pattern to the raising of the button click event).

If I modify the FirstName textbox in the UI and then tab out of the textbox and then click the Save button everything works fine. As long as the setting of the property and clicking of the button are not in the same action then all is fine.

Can anyone help?

I do re-populate the Binding Controls datasource as shown below with a Get otherwise the objects ModifiedDate property doesn't get updated and throws an exception statiing that another user is editing the object.

private void SaveSubmission(bool rebind)
{
   this.submissionBindingSource.RaiseListChangedEvents = false;
   Submission temp = submission.Clone();

   temp.ApplyEdit();

   try
      {
        submission = temp.Save();
         submission.BeginEdit();

      if (rebind)
      {
      
 //rebind the UI
      
this.submissionBindingSource.DataSource = null;
   
   //re-populate datasource so modified date is updated.
      
this.submissionBindingSource.DataSource = Submission.GetSubmission(submission.SubmissionId);
      //not updated when re-populated   
   
   submissionIdTextEdit.Text = submission.SubmissionId.ToString();
         ApplyAuthorizationRules();
      }
      }
     
catch (Csla.DataPortalException ex)
      {
}
      
catch (Exception ex)
      {
}
    
 }
      
finally
      
{
         
this.submissionBindingSource.RaiseListChangedEvents = true;
         
this.submissionBindingSource.ResetBindings(false);
   
}
}

cds replied on Wednesday, January 17, 2007

Hi CJ

That does seem a little weird (actually, a lot!), and I don't know what is wrong, but just looking at your code, you will have no idea if any exceptions are being thrown during the save as both of your catch blocks are empty, so the save could be failing and you'd have no idea.

At least put something in there, even a Debug.WriteLine to let you know what is going on.

Just my 2 cents...

Craig

C_J replied on Wednesday, January 17, 2007

Hi Craig

I do have exception code in my catch blocks, I just didn't include it in the post for verbose reasons.

To add a little more complexity to the problem. It seems to raise the button click event consistently after property change has occurred and it is the first time the button is clicked. From then on it is hit and miss.

If no property change has occured it is called every time. It is also called everytime if I tab out of the UI control so the property setter is called, and then I click the Save button.

CJ

C_J replied on Wednesday, January 17, 2007

Ok..here is some more, vey weird effects.

The problem I am describing only seems to occur when I have a break point set on the object property setter method and a breakpoint on the click event. If I remove the breakpoint on the objects property setter method then it hits the buttons click event every time. I have tested this on two computers using 2.0.3 and 2.1.1 using two different projects.

If someone else can test this out and let me know if this occurs and why it occurs it would be greatly appreciated. Is it me, VS2005, CSLA ??

On to another point...I have noticed that when calling the Save method you need to pass in a rebind bool value of true if you want to update the forms data twice or more without navigating away. If you pass in a value of false and make a change to the forms data (twice) and call the Save method, the second time the BusinessBase IsDirty flag always returns false and never persists the data back to the database. Looking at the code I cannot see how setting the rebind variable to true has anything to do with persisting the data back to the database. I can only see that it is used to rebind the object to the bindingsource object.

Can someone please explain why setting the bool value rebind when calling the Save method forces the BusinessBase IsDirty flag to return the correct result?

private void SaveEmployee(bool rebind)
{
   
this.bindingSource1.RaiseListChangedEvents = false;
   
// do the save
   
this.bindingSource1.EndEdit();
   
try
   
{
      
Employee temp = employee.Clone();
      employee = temp.Save();
      
if (rebind)
      {
         
// rebind the UI
         
this.bindingSource1.DataSource = null;
         
this.bindingSource1.DataSource = employee;
      }
   }
   
catch (Csla.DataPortalException ex)
   {
      
MessageBox.Show(ex.BusinessException.ToString(),
      
"Error saving", MessageBoxButtons.OK,
      
MessageBoxIcon.Exclamation);
   }
   
catch (Exception ex)
   
{
      
MessageBox.Show(ex.ToString(),
      
"Error Saving", MessageBoxButtons.OK,
      
MessageBoxIcon.Exclamation);
   }
   
finally
   
{
      
this.bindingSource1.RaiseListChangedEvents = true;
      
this.bindingSource1.ResetBindings(false);
   }
}

Copyright (c) Marimer LLC