Using the InfoPath Changing Event

Since Microsoft Office InfoPath was first released, I have been pushing customers with form solutions toward the Changing event as a means to address advanced data validation requirements. I am aware of the data validation already built into the form designer and generally appreciate that functionality. But there is one aspect of the out-of-the-box data validation that I just do not like: Invalid data entered by the user is still committed to the form’s data source.

In some scenarios, the fact that an InfoPath form file contains errors is not cause for alarm. Users are still prevented from submitting forms that have validation errors. However, users can still save files that have errors locally, a behavior that you might want to restrict. For these scenarios, the Changing event works well.

Two years ago, I posted an example of the Changing event, which was actually known as the OnBeforeChange event in Office InfoPath 2003. In that example I was preventing users from entering negative values into a Price field. The Office InfoPath 2007 equivalent of that code appears below. Note that “double-firing” no longer occurs in InfoPath events, thereby making it unnecessary to identify the event object operation before executing your validation code.

public void Price_Changing(object sender, XmlChangingEventArgs e)
{
    double price = double.Parse(e.Site.InnerXml);
    if (price < 0)
    {
        e.CancelableArgs.Message = "Negative numbers are not allowed.";
        e.CancelableArgs.Cancel = true;
    }
}

NOTE: The Changing event is not supported in browser forms.

Leave a Reply

Your email address will not be published. Required fields are marked *