_DataDOMEventSink_Event.OnBeforeChange Event

Definition

Occurs after changes to a form's underlying XML document have been made but before the changes are accepted.

public:
 event Microsoft::Office::Interop::InfoPath::SemiTrust::_DataDOMEventSink_OnBeforeChangeEventHandler ^ OnBeforeChange;
event Microsoft.Office.Interop.InfoPath.SemiTrust._DataDOMEventSink_OnBeforeChangeEventHandler OnBeforeChange;
member this.OnBeforeChange : Microsoft.Office.Interop.InfoPath.SemiTrust._DataDOMEventSink_OnBeforeChangeEventHandler 
Event OnBeforeChange As _DataDOMEventSink_OnBeforeChangeEventHandler 

Event Type

Examples

In the following example, the OnBeforeChange event handler is used to validate the data in a field. If the data is not valid, the ReturnStatus property of the DataDOMEventObject object is used to reject the changes.

[InfoPathEventHandler(MatchPath="/Customers/Customer/RepVisitDt", EventType=InfoPathEventType.<span class="label">OnBeforeChange</span>)]
public void RepVisitDt_<span class="label">OnBeforeChange</span>(DataDOMEvent e)
{
 IXMLDOMNode phone = thisXDocument.DOM.selectSingleNode    (@"/Customers/CustomerInfo/ContactDates/PhoneContactDt");
 if (phone.text == "")
 {
  e.ReturnMessage = "The Phone Contact Start date must be set prior to the Representative Visit date.";
  e.ReturnStatus = false;
  return;
 }
 // If the data is valid, eventObj.ReturnStatus = true.
 e.ReturnStatus = true;
 return;
}

In the following example, the OnBeforeChange event handler is used to validate the data in a field. If the data is not valid, the ReturnStatus property of the DataDOMEventObject object is used to reject the changes.

[InfoPathEventHandler(MatchPath="/Customers/Customer/RepVisitDt", EventType=InfoPathEventType.<span class="label">OnBeforeChange</span>)]
public void RepVisitDt_<span class="label">OnBeforeChange</span>(DataDOMEvent e)
{
 IXMLDOMNode phone = thisXDocument.DOM.selectSingleNode    (@"/Customers/CustomerInfo/ContactDates/PhoneContactDt");
 if (phone.text == "")
 {
  e.ReturnMessage = "The Phone Contact Start date must be set prior to the Representative Visit date.";
  e.ReturnStatus = false;
  return;
 }
 // If the data is valid, eventObj.ReturnStatus = true.
 e.ReturnStatus = true;
 return;
}

Remarks

This event handler allows users to cancel a DataDOM operation.

During the OnBeforeChange event, the form's underlying XML document is placed in read-only mode. If the ReturnStatus property of the DataDOMEventObject object is set to false, Microsoft InfoPath rejects the changes that were made and a message box is displayed to the user. If an error occurs in the code for the OnBeforeChange event, InfoPath rejects the changes and restores the data to its previous state.

Note: It is best to avoid switching views during the OnBeforeChange event; changes have not yet been accepted, and switching to another view may result in an error.

Note: In some cases, events related to changes in a form's underlying XML document may occur more than once. For example, when existing data is changed, an insert and delete operation occurs.

Note: If a validation error is encountered in the OnBeforeChange event, the document fails to load. A try/catch block in the OnLoad event can be used to catch this validation failure and to load the document despite the error.

Applies to