Skapa en händelsehanterare

Viktigt!: Den här artikeln är maskinöversatt, se ansvarsfriskrivningen. Den engelska versionen av den här artikeln finns här för din referens.

När du skriver skript för ett Microsoft Office InfoPath-formulärmall skriva du vanligtvis kod i något av formuläret mall eller data verifiering händelsehanterare. En händelsehanterare är funktionskod i ett InfoPath-formulärmall som svarar på en användare eller en ändring av XML-data i ett formulär. Hur du skapar en händelsehanterare beror på vilken typ av händelse som du vill använda. Det är viktigt att skapa händelsehanterare i designläge eftersom InfoPath automatiskt skapar händelsehanterare deklarationer i script-fil och gör poster i formulärdefinitionsfilen (xsf) som stöder en formulärmall och använda dessa händelsehanterare.

Obs!: Även om det går att skapa händelsehanterarna manuellt i skriptfilen och sedan manuellt aktivera dem genom att lägga till lämpliga element i XSF-filen, rekommenderas inte detta. Om en felaktig ändring görs i XSF-filen kan formulärmallen bli oanvändbar.

I följande tabell visas var och en av händelsehanterare InfoPath och stegvisa instruktioner för hur du skapar dem. När du skapar en händelsehanterare för en formulärmall startar på Microsoft Skriptredigeraren (MSE) och markören placeras i koden för den händelsehanteraren. Du nu kan skriva skriptet för händelsehanterare. Detaljerad information om InfoPath händelser och händelsehanterare finns i Referensinformation för InfoPath-utvecklare, som inte finns i hjälpen.

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.

Obs!: Ansvarsfriskrivning för maskinöversättning: Den här artikeln har översatts av ett datorsystem utan mänsklig inblandning. Microsoft erbjuder dessa maskinöversättningar för att hjälpa icke engelskspråkiga användare att ta del av information om Microsofts produkter, tjänster och tekniker. Eftersom artikeln är maskinöversatt kan den innehålla fel i ordval, syntax och grammatik.

Utöka dina kunskaper
Utforska utbildning
Få nya funktioner först
Anslut till Office Insiders

Hade du nytta av den här informationen?

Tack för din feedback!

Tack för din feedback! Det låter som att det kan vara bra att koppla dig till en av våra Office-supportrepresentanter.

×