Infopath : Check "Multiple Select Box" values from code behind

1.7k Views Asked by At

enter image description here

I have a Multiple Selection Box in infopath with four manual values. Now how can I check two of the options from code behind C# (Say "Option One and Option Three").

At the moment if I am adding these two from code behind by saying,

XPathNavigator elemYourTextBox = this.MainDataSource.CreateNavigator().SelectSingleNode("//my:MSList", this.NamespaceManager);
elemYourTextBox.SetValue("Option One");
elemYourTextBox.SetValue("Option Two");

only Option Two gets selected.

2

There are 2 best solutions below

1
Nathan Fisher On

It has been a while since I have used InfoPath So I may be off the mark here. The reason, I believe, is you are setting one value twice. once as Option One then as Option Two. You will need to replicate how infopath inserts the values into the text field to get the default values selected. One thing to consider is that you may need a collection of values for this to work that way you want, as that is usually how Infopath works with multiple values.

Found this article that will help Pre-select items in a Multiple-Selection List Box in InfoPath 2010

2
hilary On

Multiselect list boxes are bound to a repeating node in InfoPath. So in your code, you'll need to add an MSList node, then set it's value. I believe you can just append the node to create a copy of it. Here is a rough code sample to show the concept - this relies on one instance of field1 (which my multiselect is bound to) being present in the form, but hopefully will at least get the main idea across.

        XPathNavigator option = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:group1/my:field1", NamespaceManager);
        this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:group1", NamespaceManager).AppendChild(option);
        this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:group1/my:field1[1]", NamespaceManager).SetValue("Option 1");
        this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:group1/my:field1[2]", NamespaceManager).SetValue("Option 2");