Export (0) Print
Expand All

XDocument.OnLoad Event

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

Version Information
 Version Added:  InfoPath 2003

Syntax

expression.OnLoad(pEvent)

expression   An expression that returns a XDocument object.

Parameters

NameRequired/OptionalData TypeDescription
pEventRequiredDocReturnEventAn event object that is used during a Microsoft Office InfoPath 2007 load or submission event.

Return Value
nothing

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.

Bb229737.vs_note(en-us,office.12).gif  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:

JScript
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: initializeNodeValue and setNodeValue.

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

    // Set the node value *ONLY* if the node is empty.
    if (xmlNode.text == "")
        setNodeValue(xmlNode, strValue);
}
JScript
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;
}



Community Additions

ADD
Show:
© 2015 Microsoft