_XDocumentEventSink2_Event.OnLoad 事件

定义

在加载 Microsoft InfoPath 窗体之后,但在初始化任何视图之前发生。

public:
 event Microsoft::Office::Interop::InfoPath::SemiTrust::_XDocumentEventSink2_OnLoadEventHandler ^ OnLoad;
event Microsoft.Office.Interop.InfoPath.SemiTrust._XDocumentEventSink2_OnLoadEventHandler OnLoad;
member this.OnLoad : Microsoft.Office.Interop.InfoPath.SemiTrust._XDocumentEventSink2_OnLoadEventHandler 
Event OnLoad As _XDocumentEventSink2_OnLoadEventHandler 

事件类型

示例

在下面的示例中,OnLoad 事件处理程序用于确定表单是否已进行数字签名,如果没有,则使用函数和自定义函数的组合来初始化某些日期值:

[InfoPathEventHandler(EventType=InfoPathEventType.<span class="label">OnLoad</span>)]
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);
}

Onload 事件处理程序示例依赖于两个自定义函数: initializeNodeValuesetNodeValue

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 != "" &amp;&amp; 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;
 }
}

在下面的示例中,OnLoad 事件处理程序用于确定表单是否已进行数字签名,如果没有,则使用函数和自定义函数的组合来初始化某些日期值:

[InfoPathEventHandler(EventType=InfoPathEventType.<span class="label">OnLoad</span>)]
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);
}

Onload 事件处理程序示例依赖于两个自定义函数: initializeNodeValuesetNodeValue

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 != "" &amp;&amp; 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;
 }
}

注解

此事件处理程序允许用户取消操作。

ReturnStatus如果 对象的 属性DocReturnEventObject设置为 false,InfoPath 将取消窗体的加载。 如果错误发生在 OnLoad 事件的代码中,InfoPath 将忽略该错误并依赖于 ReturnStatus 属性。 如果没有显式设置 ReturnStatus 属性,则使用默认值 true

注意:发生 OnLoad 事件时,视图不会初始化,并且尚未加载用于视图的 XSL 转换 (XSLT) 。 直到XDocument发生 OnLoad 事件后,才会将 XDocumentsCollection 对象添加到集合中。 但在 OnLoad 事件期间,XDocument 对象是可用的。

重要提示:此事件需要完全信任安全级别。 若要设置此安全级别,请在 InfoPath 设计窗口的“工具”菜单中选择“表单选项”,然后在“安全性”选项卡上选择“完全信任”。 必须安装“完全信任”表单或者对其进行数字签名。

适用于