In the text box properties dialog click on the Data Validation button to open the Data Validation dialog window. In the Script section select OnAfterChange from the Events list and click on the Edit button. The designer adds the following Event Handler method to FormCode.cs in Visual Studio:
// The following function handler is created by Microsoft Office InfoPath. Do not
// modify the type or number of arguments.
[InfoPathEventHandler(MatchPath="/my:myFields/my:wsParam1",
EventType=InfoPathEventType.OnAfterChange)]
public void wsParam1_OnAfterChange(DataDOMEvent e)
{
// Write code here to restore the global state.
if (e.IsUndoRedo)
{
// An undo or redo operation has occurred and the DOM is read-only.
return;
}
// A field change has occurred and the DOM is writable. Write code here to respond
// to the changes.
}
At the end of the Event Handler add a method call to RequeryWebService() method. Then write the RequeryWebService() method. This code sample assumes the web service has one parameter param1.
private void RequeryWebService()
{
//Get a reference to the Web Service
IXMLDOMDocument3 wsDOM =
(IXMLDOMDocument3)thisXDocument.DataObjects["DataSourceName"].DOM;
//Set the SelectionNamespaces so that you can find the correct field
wsDOM.setProperty("SelectionNamespaces",
"xmlns:dfs=\"http://schemas.microsoft.com/office/infopath/2003/dataFormSolution\""
+ " xmlns:s0=\"http://tempuri.org/\"");
//Set Web Service parameters
wsDOM.selectSingleNode
("/dfs:myFields/dfs:queryFields/s0:DataSourceName/s0:param1").text =
thisXDocument.DOM.selectSingleNode("/my:myFields/my:wsParam1").text;
//Requery the webservice datasource
thisXDocument.DataObjects["DataSourceName "].Query();
}
** Caution **
You will also want to add a rule via the property dialog to the drop down list (field2) to set the value to "" otherwise when the user has selected a value from the list and then enters different search criteria into the text box, the selected value remains in the drop down list.
Preview the form et voilà! The user can enter a value into the text box and when the control loses focus the OnAfterChange event is fired, setting the web service parameter with the value from the text box, querying the web service and populating the drop-down list box with values returned.
This example be can easily extended to handle multiple web service parameters, and even to create cascading drop down lists.