Programmatically retrieve the value of another field in the same row of a repeating table

Applies to: InfoPath 2003

Use the Source property of the DataDOMEvent object in the OnAfterChange event of a field in a repeating table to retrieve the value of another field in the same row of the repeating table using JScript.

ADVERTISEMENTS

Problem

You have added an OnAfterChange event handler on the FirstName field of a Repeating Table and would like to retrieve the value of the LastName field that is in the same row as the FirstName field.

Your Repeating Table looks as follows when you preview the InfoPath form:

The XML for a row in the Repeating Table looks like:

<Player ID="10" FirstName="Chipper" LastName="Jones" No="10" Position="3B" />

Solution

Use the Source property of the eventObj object that is being passed to the OnAfterChange event to retrieve the value for LastName in the OnAfterChange event handler for the FirstName field:

var lastName = "";

if (eventObj.Source.parentNode)
{
lastName = eventObj.Source.parentNode.selectSingleNode("../@LastName").text;
}

Discussion

The Source property of the DataDOMEvent object returns a reference to the XML Document Object Model (DOM) where the data validation event is occurring.

If you change the text in the FirstName field in the Repeating Table, this action will trigger the OnAfterChange event on this field, and eventObj.Source in the OnAfterChange event handler will return a #text node. You can check this by adding the following code to the OnAfterChange event handler:

XDocument.UI.Alert(eventObj.Source.nodeName);

So to get to the node for the FirstName field attribute you would have to use

eventObj.Source.parentNode

since the FirstName node will be the parent of the #text node being changed.

Once you have the node for the FirstName field attribute, it is only a matter of using an XPath expression to navigate upwards to retrieve the parent node of FirstName and then back downwards to get to the node for the LastName field attribute as in the following XPath expression:

../@LastName

Suppose just for a minute that the XML for your Repeating Table does not consist of field attributes, but of field elements. For example, the XML for a row in your Repeating Table looks like:

<Player>
<FirstName>Chipper</FirstName>
<LastName>Jones</LastName>
<No>10</No>
<Pos>3B</Pos>
</Player>

In this case you would have to use the following code to retrieve the value of LastName in the OnAfterChange event handler for the FirstName field:

var lastName = "";

if (eventObj.Source.parentNode)
{
lastName = eventObj.Source.parentNode.selectSingleNode("../LastName").text;
}

Tip

If you have added a Data Connection to your InfoPath form and named it Players, you can retrieve and display the entire XML for this secondary data source using the following code:

XDocument.UI.Alert(XDocument.GetDOM("Players").xml);
or
XDocument.UI.Alert(XDocument.DataObjects["Players"].DOM.xml);
This can be very useful to find out what the XML structure of the secondary data source looks like as well as which namespaces must be used to retrieve nodes when using XPath expressions.

 


Related InfoPath Articles:

 

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services