Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#

How to create dynamically populated dropdown in an InfoPath Form

Rate me:
Please Sign up or sign in to vote.
1.00/5 (3 votes)
21 Jul 2010CPOL2 min read 51.8K   279   18  
How to create dynamically populated dropdown in an InfoPath Form

Introduction

Assume that a form needs to have a drop down list whose values are dynamically loaded dependent on the user's selection in another control and the solution should avoid problems with writing and maintaining a complex DOM manipulation script. Assume that the values for the drop down list should come from an external source like a database table or an XML file.

This document will go into the Indecomm Knowledge Base repository.

Sample Scenario

The user is presented with a Country drop down list. Depending on his choice, the City drop down list should be populated with Cities that are in the selected country.

Solution

An external data source will be created to access a relational data table or an XML file. Entries for the City drop down are filtered using a defined filter.

Step by step walk-through

  1. Create an XML file as follows and save it as Options.xml
    <?xml version="1.0" encoding="utf-8" ?>
    <Options>
    <Country CountryID="US" CountryName="United States">
        <City CityID="LAX" CityName="Los Angeles" /> 
        <City CityID="SEA" CityName="Seattle" />
    </Country>
    <Country CountryID="CA" CountryName="Canada">
        <City CityID="WPG" CityName="Winnepeg" />
        <City CityID="ONT" CityName="Ontario" />
    </Country>
    </Options>
  2. Open a new blank InfoPath form in design mode
  3. Save it as Country-City.xsn
  4. Go to Tools | Data Connections | Add | RecieveData | XMLDocument
  5. Select the Options.xml file
  6. Check Automatically Retrieve Data and click Finish.
    Note: This will allow you not to reload the Option.xml file.
  7. Create two Drop down boxes, named ddCountry and ddCity
  8. Double-click on ddCountry
  9. Check Lookup values in a data connection…
  10. Select Options in the Data Connection drop down.
  11. Click on Entries, select Country, click OK
  12. Select Value as CountryID and Name as CountryName
  13. Select Apply | OK
  14. Double-click on ddCity
  15. Check Lookup values in a data connection…
  16. Select Options in the Data Connection drop down.
  17. Click on Entries. Select City then click FilterData | Add
  18. Select City, is equal to,and ddCountry from Select a field or group | Main
  19. Press OK 3 times
  20. Select Value as CityID and Name as CityName
  21. Select Apply | OK
  22. We’re done..! Preview the form.

Note: If you would like to publish this form to public, you need to remove the security restrictions.

  1. Go to Menu | Tool | Forms Options | Security
  2. Uncheck Automatically determine security.
  3. Check the Full Trust option
  4. Check Sign this form and create or use a certificate.

Install this certificate on the client computer where you will use the form.

Conclusion

This simple solution helps to avoid using a DOM manipulation script, which is complex.

Contact

For any further information or queries contact me at firozozman@hotmail.com

My Blog: www.firozozman.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
Firoz Mohammed (Firoz`Ozman) |
MCSD.Net, MCTS (BizTalk 2004 & 2006)
Avanade Inc: www.avanade.com |
Toronto, Canada |

Comments and Discussions

 
-- There are no messages in this forum --