how to set listbox value dynamically?

D

daska

Hi

I am working on an InfoPath browser based form. I have created a multiselect
list box that contains two nodes, one for a checkbox and the other for a text
box. I succeeded in adding new rows programmatically but now I dont know how
to set the values of the inner nodes. For example, I copied following code
from the MS site:
XPathNavigator DOM = this.MainDataSource.CreateNavigator();
XPathNodeIterator nodes = DOM.Select("/my:myFields/my:Options/my:Option");
XPathNavigator nodesNavigator = nodes.Current;

XPathNodeIterator nodesText =
nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

while (nodesText.MoveNext())
{
Console.Write(nodesText.Current.Name);
Console.WriteLine(nodesText.Current.Value);
}

This code shows how to read from the inner nodes but How can we set the
values of these inner nodes?

Thanks,

Daska
 
S

S.Y.M. Wong-A-Ton

You can use

nodesText.Current.SetValue("YourValue");

Double-check the node type before you do, because you may get an error if
the node is not of type "Text". So do something like

if (nodesText.Current.NodeType == XPathNodeType.Text)
{
nodesText.Current.SetValue("YourValue");
}
 
D

daska

Thank you so much. I am almost there. I have two descendants. One is checkbox
and the second is a text box. The current code populates the checkboxes. How
to populate the text boxes? Kindly look at the code below:

XPathNodeIterator nodesText =
nodesNavigator.SelectDescendants(XPathNodeType.Text, false); --> This only
selects the checkbox!!! (How to select the textbox here???)

while (nodesText.MoveNext())
{
//Console.Write(nodesText.Current.Name);
//Console.WriteLine(nodesText.Current.Value);
if (nodesText.Current.NodeType ==
XPathNodeType.Text)
{
nodesText.Current.SetValue("true"); --> This
sets the checkboxes value!!!!
}

}

I appreciate your help!! Thanks once again.

Daska

p.s. I love your articles! There are very few InfoPath experts out there!
You are one of them!
 
S

S.Y.M. Wong-A-Ton

I'm not sure in what context the code you copied was being used, so not sure
why the Text nodes were retrieved, but try this instead:

XPathNodeIterator nodesText =
nodesNavigator.SelectDescendants(XPathNodeType.Element, false);
while (nodesText.MoveNext())
{
if (nodesText.Current.Name == "my:field1")
nodesText.Current.SetValue("true");

if (nodesText.Current.Name == "my:field2")
nodesText.Current.SetValue("MyValue");
}

or you could change the entire lot to this:

XPathNavigator DOM = this.MainDataSource.CreateNavigator();
XPathNodeIterator nodes = DOM.Select("/my:myFields/my:Options/my:Option",
NamespaceManager);
while (nodes.MoveNext())
{
nodes.Current.SelectSingleNode("my:field1",
NamespaceManager).SetValue("true");
nodes.Current.SelectSingleNode("my:field2",
NamespaceManager).SetValue("MyValue");
}

where my:field1 is your checkbox and my:field2 your text box.

I'm glad you enjoy the articles I publish. Thank you for your positive
feedback.
 
D

daska

I can't thank you enough. You proved to be a Messiah. Thank you very very much.

Regards

Daska
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top