You might remember from an earlier post that the application already has a button for adding a new presenter to the database. Clicking the button shows the popup window; but instead of displaying blank fields ready for new data, it shows the details of whichever presenter’s details we had just viewed. In addition, clicking the OK button of the popup window calls a function that tries to update the information about a presenter who already exists. To solve these problems, a means is needed to keep track of whether existing data is being altered, or whether new information is being added. To achieve this, I first of all added a global variable IsNew, and set it equal to true. When details about an existing presenter are being displayed, I want to set IsNew equal to false. I therefore added the line “IsNew = false;” to the onCommand function:
I then made two additions to the command initialising the ModalPopup: a handler for the popup’s cancel button, and a handler for its showing event.
The showing event is raised just before the popup is displayed on the screen. The PopupShowing function, which handles the event, is as follows:
If the IsNew global variable equals true, the function binds the PresenterDetails DataView to an empty object, which has the effect of setting all its fields to null. When the New Presenter button is clicked, the resulting Popup looks like this:
Lots of nulls! To remove them, a convert function can be used:
This function takes one parameter, and returns an empty string if it equals null, and returns the parameter unchanged if it is not equal to null. This function is used in the details view as follows:
The popup window now looks somewhat better:
The same converter can be used in the master view:
When the OK button in the popup window is clicked, the SubmitChange function is called. When that function was described in the last post, it had only to update an existing record. Now, it must first check whether it needs to perform an update or an insert.
The first two lines retrieve the data from the details view, which is required in either case. If IsNew equals true, an insert is performed with the insertEntity method. This takes two parameters, the data to be inserted, and the name of the data type, which matches the name of the database table into which the data should be inserted. The saveChanges method (which was described in the previous post) is then called. Whichever operation is performed, the value of IsNew must be reset to true.
If the cancel button in the popup window is clicked, the CancelChange function is called. No changes need to be made to the data, but IsNew again needs to be reset to true.
In my last post, I said I had three reasons for not binding the details view to the master view, and I gave two of them. The third reason relates to inserts. With the two views bound together, when the fields in the details view are cleared, I assume the associated data in the data context will also be cleared. It seemed to me that this was likely to cause problems, and I could not work out how to temporarily disable data binding.