Sunday, March 18, 2018

Create an event handler

Create an event handler

When writing script for a Microsoft Office InfoPath form template, you usually write code in one of the form template or data validation event handlers. An event handler is the function code in an InfoPath form template that responds to a user action or a change to the XML data in a form. How you create an event handler depends on the type of event that you want to use. It's important to create the event handler in design mode because InfoPath automatically creates the declarations of the event handlers in the script file and makes entries in the form definition (.xsf) file that enable a form template to use those event handlers.

Note: Although it is possible to create the event handlers manually in the script file and then enable them by manually adding the appropriate elements to the .xsf file, it is not recommended. If an invalid change is made to the .xsf file, the form template may not be usable.

The following table lists each of the InfoPath event handlers and the steps for creating them. When you create an event handler for a form template, InfoPath starts the Microsoft Script Editor (MSE) and places the cursor in the code for that event handler. You can then write the script for the event handler. For detailed information about InfoPath events and event handlers, see the InfoPath Developer's Reference, which is available in MSE Help.

Event handler

Description

Steps for creating

OnAfterChange

This event runs after a user changes the value in the specified control bound to a field. For example, you can display a custom HTML task pane after a user selects an item in a list box control. This event runs after the OnValidate event.

Right-click a control on the form template or a field in the Data Source task pane, point to Programming on the shortcut menu, and then click On After Change Event.

OnAfterImport

This event runs after the user successfully merges several forms into one form.

Manually add the OnAfterImport event handler declaration directly to the script file by using MSE. This event handler cannot be created in design mode.

OnBeforeChange

This event runs after a user changes the value in a control and before the value is added to the specified field bound to that control. For example, you can prevent a user from adding an additional row before filling out the last row in a repeating table.

Right-click a control on the form template or a field in the Data Source task pane, point to Programming on the shortcut menu, and then click On Before Change Event.

OnClick

This event runs when a user clicks a button associated with this event. This event is available after you add a button to the form template. For example, you can perform a complex calculation by using the values in fields in the main data source when a user clicks the button.

Double-click the button control and then on the General tab of the Button Properties dialog box, click Edit Form Code.

OnContextChange

This event runs when the focus changes in the form. For example, this event runs when a user navigates from a text box to a date picker, or when a user switches views. This event occurs after all other events have occurred.

On the Tools menu, point to Programming, and then click On Context Change Event.

OnLoad

This event runs whenever a user creates a new form or opens an existing form based on your form template. For example, you can copy a list of items from a secondary data source to the main data source when a user opens the form.

On the Tools menu, point to Programming, and then click On Load Event.

OnMergeRequest

This event runs when forms based on your form template are merged. For example, you can display the number of forms that were merged to give a user a status during the merge operation.

On the Tools menu, click Form Options. In the Category list, click Advanced. Under Merge forms, select the Merge using custom code check box, and then click Edit.

OnSaveRequest

This event runs when a user saves a form based on your form template. For example, you can save the form based on this form template to more than one location when a user saves the form. InfoPath adds two additional lines of code to this event: a line that saves the form and another line that tells you if InfoPath successfully saved the form.

On the Tools menu, click Form Options. In the Category list, click Open and Save. Under Save behavior, select the Save using custom code check box, and then click Edit.

OnSign

This event runs when a user applies a digital signature to a form. For example, you can add additional data to the digital signature of an expense report form when a user signs it.

On the Tools menu, point to Programming, and then click On Sign Event.

OnSubmitRequest

This event runs when a user submits his or her form data. For example, you can submit the form data to a secure Web service when the user clicks Submit on the File menu. Since InfoPath does not support a data connection to a secure Web service, you need to add a custom data connection that works with your secure Web service.

On the Tools menu, click Submit Options. Select the Allow users to submit this form check box, click Perform custom action using Code, and then click Edit Code.

OnSwitchView

This event runs when a user opens a form based on your form template or switches to another view in the form. For example, when a user switches to a specific view, you can create an e-mail message that contains data from the fields in the form's main data source.

On the Tools menu, click Programming, and then click On Switch View Event.

OnValidate

This event runs when a user changes the value in a control bound to a field. For example, when the user changes the value in a specific control, you can calculate a new value for another control by using the number that the user entered and a number that is in the script. This event occurs after the OnBeforeChange event handler runs.

Right-click a control on the form template or a field in the Data Source task pane, point to Programming on the shortcut menu, and then click On After Validate Event.

OnVersionUpgrade

This event runs when a user opens an existing form and the version number of the form template used by the existing form is older than the version number of the form template in the publish location. When a user opens an existing form, InfoPath checks the version number of the form template associated with the existing form. If the form template has a newer version number, InfoPath runs the OnVersion event handler when the user opens the existing form. For example, imagine that you add a new field to an existing form template and republish it, and you want the new field to be added to the main data source if a user opens an existing form. This event adds the new field to the main data source when a user opens an existing form.

On the Tools menu, click Form Options. In the Category list, click Versioning. In the On version upgrade list, click Use custom event, and then click Edit.

No comments:

Post a Comment