Secondary Data Source Web Service Parameters using C# at Runtime in InfoPath
page 3 of 3
by Michelle Beall
Feedback
Average Rating: 
Views (Total / Last 10 Days): 23624/ 21

Handling the OnAfterChange Event

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.


 


View Entire Article

User Comments

Title: Managed Code Question   
Name: Keith B
Date: 2005-02-06 1:19:33 AM
Comment:
So far, all of my infopath developement has used Jscript, and the SP1 SDK provides some very useful JScript routines such as sorting an xml document object, etc. If I used managed code such as C#, or VB.net, will I need to re-create that Jscript funtionality, or can I use Jscript along with the managed code?
Title: Which Web Service does this example use?   
Name: Keith B
Date: 2005-02-06 1:15:29 AM
Comment:
Thank you for the tutorial. I'm a newbie as far as Web Services are concerned. Here are some questions:
1) Your tutorial specifies a web service running under IIS 6.0. Why is that important? There are many online web services and some are not running under IIS 6.0. How would the example need to be different to work with a non-IIS 6.0 Web site?

2) I cannot find any reference in the tutorial to a specific web service. Probably, I do not know enough about Web Services to identify what aspects of the code relate to a specific web service. I guess another form of this question would be: If I followed the tutorial exactly and pasted your code from the code examples. Which web service would I be accessing when I run the example? Or, what do I need to do to provide the web service that this example uses?

Thanks again for creating this tutorial.
Title: Secondary Data Source Web Service Parameters using C# at Runtime in InfoPath   
Name: Anbukannan
Date: 2004-08-24 5:04:16 AM
Comment:
It would be help for a web service newbie like me if this article covers abt writing web service bound as the secondary data source

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-07-23 3:59:56 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search