Share via


_XDocumentEventSink2_Event.OnLoad Event

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

Namespace:  Microsoft.Office.Interop.InfoPath.SemiTrust
Assembly:  Microsoft.Office.Interop.InfoPath.SemiTrust (in Microsoft.Office.Interop.InfoPath.SemiTrust.dll)

Syntax

'Declaration
Event OnLoad As _XDocumentEventSink2_OnLoadEventHandler
'Usage
Dim instance As _XDocumentEventSink2_Event
Dim handler As _XDocumentEventSink2_OnLoadEventHandler

AddHandler instance.OnLoad, handler
event _XDocumentEventSink2_OnLoadEventHandler OnLoad

Remarks

This event handler allows users to cancel an operation.

If the ReturnStatus property of the DocReturnEventObject object is set to false, InfoPath cancels the loading of the form. If an error occurs in the code for the OnLoad event, InfoPath ignores it and relies on the ReturnStatus property. 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 XDocumentsCollection collection until after the OnLoad event has occurred. However, the XDocument object is available during the OnLoad event.

Important

This event requires a Full Trust Security Level. To set this Security Level, choose Form Options from the Tools menu in the InfoPath design window, then on the Security tab select Full Trust. A Full Trust form must be installed or digitally signed.

Examples

In the following example, 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 functions and custom functions:

[InfoPathEventHandler(EventType=InfoPathEventType.OnLoad)]
public void OnLoad(DocReturnEvent e)
{ 
 // Avoid DOM updates when the document has been digitally signed.
 if (thisXDocument.IsSigned)
 {
  return;
 }

 string today = thisXDocument.Util.Date.Today().ToString();
 initializeNodeValue("/sls:salesReport/sls:date", today);
}

This Onload event handler example depends on two custom functions: initializeNodeValue and setNodeValue.

private void initializeNodeValue(string xpath, string strValue)
{
 IXMLDOMNode xmlNode = thisXDocument.DOM.selectSingleNode(xpath);
 // Set the node value *ONLY* if the node is empty.
 if (xmlNode.text == "")
 {
  setNodeValue(xmlNode, strValue);
 }
}

private void setNodeValue(IXMLDOMNode xmlNode, string strValue)
{   
 if (xmlNode == null)
 {
  return;
 }

 // The xsi:nil needs to be removed before we set the value.
 if (strValue != "" && xmlNode.attributes.getNamedItem("xsi:nil") != null)
 {
  xmlNode.attributes.removeNamedItem("xsi:nil");
 }

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

See Also

Reference

_XDocumentEventSink2_Event Interface

_XDocumentEventSink2_Event Members

Microsoft.Office.Interop.InfoPath.SemiTrust Namespace