Reset a boolean field in InfoPath

Want to reset a boolean field, with an initial value of (empty), to its initial state? Read on!

The initial value of an ’empty’ boolean field in InfoPath:

<my:boolean xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:nil=”true“></my:boolean>

After you select one of the radiobuttons the textnode of the element gets a value indicating whether true or false was selected:

<my:boolean xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“>true</my:boolean>

The important thing to notice here, is the removal of the attribute ‘xsi:nil=”true”‘. If you want to reset this field to its initial state then you need to do two things: remove the textnode and recreate the ‘xsi:nil’ attribute.

Let’s add a button (resetBtn) to the form and give it some code-behind:

public void resetBtn_Clicked(object sender, ClickedEventArgs e)
{
    resetNode("/my:mijnVelden/my:boolean");
}
private void resetNode(string xpath)
{
    XPathNavigator xnMain = MainDataSource.CreateNavigator();
    XPathNavigator node = xnMain.SelectSingleNode(xpath, NamespaceManager);
    XPathNavigator textNode = node.SelectSingleNode("text()");
    if (textNode != null)
    {
        textNode.DeleteSelf();
        node.CreateAttribute("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true");
    }
}

And that’s about it.

Download an example here: InfoPath Form Template – resetBoolean

Leave a Reply

Your email address will not be published. Required fields are marked *