I've been fighting with this for a couple of days and cannot see the forest through the trees...
I have a view that contains a single repeating section. New sections should be inserted or deleted based on checkbox values on other views. When a box is checked and a section inserted, I am using VB.net to set a "context" attribute on the repeating section to a value corresponding to the checkbox that spawned the insertion, and displaying the value in the section entry via an expression box control.
Here's the repeating section data source tree:
my:Root
... my:DesignSurvey
.... my:Divisions
..... my:Opener
...... my:OpenerEntry (this repeats)
my:OpenerEntry has a series of boolean attributes (more checkboxes) and one text attribute, my:Context, to keep track of which checkbox from the other view spawned the insertion of whichever specific entry.
I've got the first part working - that is, the part where you check a box, and the onAfterChange event walks the dom and inserts a new repeating section, and then sets the context attribute:
<InfoPathEventHandler(MatchPath:="/my:XPF/my:DesignSurvey/my:Divisions/my:Part/@my:PartOpener", EventType:=InfoPathEventType.OnAfterChange)> _
Public Sub PartOpener_attr_OnAfterChange(ByVal e As DataDOMEvent)
' Write code here to restore the global state.
If (e.IsUndoRedo) Then
' An undo or redo operation has occurred and the DOM is read-only.
Return
ElseIf (Not (e.Site Is e.Parent)) Then
Return
ElseIf ("Insert" <> e.Operation) Then
Return
End If
'A field change has occurred and the DOM is writable. Write code here to respond
'to the changes.
Dim domDoc As IXMLDOMDocument = thisXDocument.DOM
If e.Source.text = "1" Then
Dim oTemplateGrp As IXMLDOMElement = domDoc.selectSingleNode("/my:XPF/my:DesignSurvey/my:Divisions/my:Opener/my:OpenerEntry")
Dim oTemplateNode As IXMLDOMElement = oTemplateGrp.cloneNode(True)
domDoc.selectSingleNode("/my:XPF/my:DesignSurvey/my:Divisions/my:Opener").appendChild(oTemplateNode)
Dim chkExpression As IXMLDOMAttribute = domDoc.selectSingleNode("my:XPF/my:DesignSurvey/my:Divisions/my:Opener/my:OpenerEntry/@my:Context")
If chkExpression.value = "" Then
Dim parentOpener As IXMLDOMElement = domDoc.selectSingleNode("my:XPF/my:DesignSurvey/my:Divisions/my:Opener")
parentOpener.removeChild(oTemplateGrp)
End If
Dim contextNode As IXMLDOMElement = domDoc.selectSingleNode("/my:XPF/my:DesignSurvey/my:Divisions/my:Opener/my:OpenerEntry")
contextNode.setAttribute("my:Context", "Part")
However, I have not been able to figure out how to navigate back to the correct repeating section if I go back to the view where I originally checked the box to insert the entry and uncheck the box... I thought I could do this by checking the context attribute value, but found that whatever value assigned to the context attribute from the last checkbox entry appears to have been reassigned to the context attribute in all of the entries despite the persistence of the correct text in the expression box in the view...
I checked this via the second part of the code shown above:
ElseIf e.Source.text = "0" Then
thisXDocument.UI.Alert(e.Source.text)
Dim nodeList As DOMNodeList = domDoc.selectNodes("/my:XPF/my:DesignSurvey/my:Divisions/my:Opener/my:OpenerEntry")
thisXDocument.UI.Alert(nodeList.length)
Dim node As IXMLDOMNode
Dim contextNode As IXMLDOMAttribute = domDoc.selectSingleNode("/my:XPF/my:DesignSurvey/my:Divisions/my:Opener/my:OpenerEntry/@my:Context")
Dim attrValue As String = contextNode.text
For Each node In nodeList
thisXDocument.UI.Alert(attrValue)
Next
End If
Return
End Sub
So for instance, if I check boxes to insert a new repeating section for Appendix, Part, Chapter, and Box, in that order, I get 4 repeating sections, but when I try to display the values of the context attr in a UI.alert, I get whatever value corresponds to the last checkbox checked (in this case, box) 4 times - which means I cannot actually get back to the entry that was originally spawned by the checkbox to remove the node.
Any ideas? Am I crossing my XPath apples and oranges and winding up with grapes? I'm happy to provide more information if this is not enough to start with.
Thanks,
Jean K.