Show All

# OnLoad Event

Occurs after a Microsoft Office InfoPath 2003 form has been loaded, but before any views have been initialized.

Function XDocument::OnLoad(ByRef pEvent As DocReturnEvent)

pEvent Required DocReturnEvent. A reference to the DocReturnEvent object.

## Remarks

This event handler allows users to cancel an operation.

If the ReturnStatus property of the DocReturnEvent object is set to False, InfoPath cancels the loading of the form. If an error occurs in the scripting code for the OnLoad event handler, InfoPath ignores it and relies on the ReturnStatus property of the DocReturnEvent object. If the ReturnStatus property is not explicitly set, the default value of True is used.

Note When the OnLoad event occurs, the view is not initialized and the XSL Transformation (XSLT) used for the view is not yet loaded. The XDocument object is not added to the XDocuments collection until after the OnLoad event has occurred. However, the XDocument object is available during the OnLoad event.

## Example

In the following example from the Sales Report sample form, the OnLoad event handler is used to determine whether the form has been digitally signed, and if it hasn't, to initialize some date values using a combination of scripting functions and custom functions:

    function XDocument::OnLoad(objEvent)
    {
       // Avoid DOM updates when the document has been digitally signed.
       if (XDocument.IsSigned)
          return;

       var today = new Date();
       initializeNodeValue("/sls:salesReport/sls:date", getDateString(today));
       initializeNodeValue("/sls:salesReport/sls:year", today.getFullYear());
    }

This Onload event handler example depends on two custom functions, which are also from the Sales Report sample form: initializeNodeValue and setNodeValue.

    function initializeNodeValue(xpath, strValue)
    {
        var xmlNode = getNode(xpath);

        // Set the node value *ONLY* if the node is empty.
        if (xmlNode.text == "")
            setNodeValue(xmlNode, strValue);
    }

    function setNodeValue(xpath, value)
    {
        var xmlNode = getNode(xpath);

        if (!xmlNode)
            return;

        // The xsi:nil needs to be removed before we set the value.
        if (value != "" && xmlNode.getAttribute("xsi:nil"))
            xmlNode.removeAttribute("xsi:nil");

        // Setting the value would mark the document as dirty.
        // Let's do that if the value has really changed.
        if (xmlNode.text != value)
            xmlNode.text = value;
    }

©2003-2004 Microsoft Corporation. All rights reserved. Permission to copy, display and distribute this document is available at: http://msdn.microsoft.com/library/en-us/odcXMLRef/html/odcXMLRefLegalNotice.asp