The drop-down list box control functions similarly to the select element of HTML and XHTML. The options offered in an InfoPath drop-down list box are specified in the Drop-Down List Box Properties window (see Figure 4.10). If the information to populate the drop-down list box is not provided from a defined data source, you can specify a data type for the content of the drop-down list box. Figure 4.10. The Drop-Down List Box Properties window. Validation In the Validation section of the Drop-Down List Box Properties window, you can specify whether the list box can be blank. In other words, if the Cannot Be Blank check box is checked, the user must specify a value from the drop-down list box. It is also possible to add a validation rule for the drop-down list box. (Validation is discussed in detail in Chapter 10.) Entering Data Values The drop-down list box entries can be sourced either by entering a list of values in the List Box Entries section of the Drop-Down List Box Properties window or from a data source. Figure 4.11 shows a range of priorities added to the Drop-Down List Box Properties window in the DropDownExample.xsn form template. To add an entry, click the Add button to open the Add Choice window. In the top box, enter the value for an entry, and in the lower text box, enter text to be displayed to the user for that entry. Click OK to add the entry to those listed in the List Box Entries section. Figure 4.11. Adding literal entries for a drop-down list box. The entries added to the List Box Entries section can be reordered using the Move Up and Move Down buttons . Entries can be edited using the Modify button. Figure 4.12 shows the preview of the drop-down list box control used to specify task priority. Figure 4.12. Preview of the drop-down list box control. Using a Secondary Data Source The other option for populating a drop-down list box control is to provide entries from a database, XML Web service, or an XML file. This allows options to be updated without altering the form template. When that approach is the desired option, as it often will be in a production setting, select the Look Up in a Database, Web Service, or File radio button. Selecting that option concealsbut doesn't deleteany options entered by hand as described earlier. To add a new secondary data source, follow these steps: -
Click on the Secondary Data Source button. This opens the Secondary Data Sources window. -
To add a new secondary data source, click on the Add button; the Data Source Setup Wizard opens (see Figure 4.13). Figure 4.13. The Data Source Setup Wizard. -
In this example, we will use a file located at c:\IKS\Ch04\Priorities.xml as the secondary data source. In the Data Source Setup Wizard, choose the XML Data File option and click the Next button. -
In the next window, either type in the path stated in the previous sentence or click the Browse button and locate the specified file. The content of the file Priorities.xml is shown in Listing 4.2. Listing 4.2. A List of Priorities for a Drop-Down List Box ( Priorities.xml ) <Priorities> <Priority value="High">High Priority</Priority> <Priority value="Medium">Medium Priority</Priority> <Priority value="Low">Low Priority</Priority> </Priorities> -
When the secondary data source has been selected, the file path is displayed in the Data Source Setup Wizard. Click Next. -
In the next window, we receive the message You have successfully set up your data source. You might want to review and possibly change the default name provided for the data source (see Figure 4.14). Make sure that the check box specifying the connection to the data source when the form is opened is checked. Figure 4.14. The final window when the data source has been set up correctly. -
Click the Finish button. A Microsoft Office InfoPath window will open telling you, quite accurately, that the Priorities.xml file is not part of the form template and asking if you want to add it to the form template. In a production situation, you would select the Yes option because you would want the secondary data source to be included in the form template. In this example, we will select the No option. The window closes , and we are returned to the Secondary Data Sources window where the Priorities secondary data source is displayed (see Figure 4.15). Figure 4.15. The Secondary Data Sources window after a secondary data source has been added. -
At this stage, the secondary data source has been specified, but we have not yet specified how the parts of the Priorities.xml file are to be used in the secondary data source. Click Close in the Secondary Data Sources window, which returns you to the Drop-Down List Box Properties window. The Priorities data source should be specified in the Data Source drop-down menu in the List Box Entries section (see Figure 4.16). Figure 4.16. The Drop-Down List Box Properties window with a secondary data source specified. -
Click on the square button to the right of the Entries text box. This opens the Select a Field or Group window (see Figure 4.17). Figure 4.17. The Select a Field or Group window. -
Click on the node beside Priorities and then select Priority. The Priority element in the Priorities.xml file is a repeating field, so it is suitable for use to provide the options in the drop-down list box control. -
Click OK. The XPath expression /Priorities/Priority is displayed in the Entries text box. The Value text box and the Display Name text box each contain a single dot (see Figure 4.18). The dot (.) is an XPath abbreviation meaning the context node, and is an abbreviation for self::node() . Figure 4.18. The Drop-down List Box Properties window with XPath expressions defined. -
Both the Value text box and the Display Name text box contain the same value. We want to derive the value from the value attribute of the Priority element in the secondary data source. Therefore, we modify the value in the Value text box to read @value , meaning the value of the value attribute. If the attribute had been called myValue , we would have written @myValue . If you don't feel confident writing even simple XPath expressions directly, you have the option to let InfoPath create the XPath expression for you. Click the square button to the right of the Value text box (the cursor in Figure 4.18 is next to that box) and select the value attribute in the Select a Field or Group window and click OK. We are now ready to preview the simple drop-down box control. Click the Preview Form button in the Standard toolbar, or use the Alt+P keyboard shortcut. Figure 4.19 shows the onscreen appearance when the control is ready for a value to be selected. Figure 4.19. Preview of a simple drop-down box control. This might seem like a lengthy process for a relatively simple result. Rest assured that it takes much longer to read about (and write about) than it takes to perform the task after you are up to speed. Similar techniques can be applied to use an XML Web service or a relational database as a secondary data source. |