Microsoft InfoPath 2010
The official blog of the Microsoft InfoPath team

What button was clicked?

What button was clicked?

  • Comments 3

Another question that we get asked a lot:

I include a Repeating Section control in my form, containing a Button and a Text Box. At runtime, a user inserts 5 items. How do I know which Button was clicked so I can get to the Text Box’s value?

That’s actually the wrong question to ask. Strictly speaking, InfoPath doesn’t know the difference between the Buttons. They are all identical clones. What’s different about each button is its context within the view, and the view is mapped to the data. (In InfoPath it always comes back to the data.)

If your schema looks like this:

  • myNodes
    • group1
      • group2 (repeating)
        • field1

Your view probably looks something this:

[View Structure]

Since the Button control is inside the Repeating Section which is bound to group2, we say that the Button’s context is group2.

So what?

When the Button event is received, it contains a DocActionEvent object. The event object has a Source property. And this property is the Button’s context node – the specific group2 instance the Button is inside.

So at this point, you can just use eventObj.Source to refer to data relative to the Button. Probably the easiest thing to do is just do some old school debugging using alerts. Here’s how to make sure you’re getting the context right:

function CTRL3_5::OnClick(eventObj)

{

// Write your code here

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

}

Here’s what you’d get:

    <my:group2 xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2004-09-14T16:02:14">

        <my:field1>abc</my:field1>

    </my:group2>

And to wrap things up, to get the text inside the Text Box, you need to use eventObj.Source.selectSingleNode():

function CTRL3_5::OnClick(eventObj)

{

// Write your code here

XDocument.UI.Alert( eventObj.Source.selectSingleNode("my:field1").text );

}

Leave a Comment
  • Please add 4 and 4 and type the answer here:
  • Post
Page 1 of 1 (3 items)