CodeSnip: Add Row to InfoPath Repeating Table on Button Click
page 1 of 1
Published: 04 May 2005
Unedited - Community Contributed
Abstract
This codesnip demonstrates how, in response to a button click, we add a row to a repeating table in InfoPath. We will be using C#.
by Michelle Beall
Feedback
Average Rating: 
Views (Total / Last 10 Days): 17728/ 22

Adding a row to a repeating table is as simple as adding the appropriate nodes and attributes to the XML document. InfoPath is just XML. The UI provides some very rich controls, but those controls cannot be interfaced with directly. It is the XML structure that needs to be edited. Any changes in the underlying XML document will be reflected automatically in the controls in the UI.

1.      To set up the example, create a new blank InfoPath form from Visual Studio .NET, using C#.

2.      Edit the Main DOM Data source so that it looks similar to Figure 1.

3.      Drag the Repeating Table element onto the blank form canvas and choose repeating table as the control to use.

4.      Add a Button control beneath the repeating table on the form canvas, so the form looks similar to Figure 1.

Figure 1 – Form Layout and Main DOM Data Structure

5.      Open the button's properties window by double-clicking on the button

6.      Click on the "Edit Form Code" button.

7.      An event handler stub is added to the FormCode.cs file for the button’s OnClick event. Add the code listed in Listing 1 to the body of the new method.

Listing 1 – Button OnClick event

// get a reference to the node the repeating table is bound to
IXMLDOMNode row = thisXDocument.DOM.selectSingleNode("//my:repeatingtable");
// make a copy of the node by cloning it
row = row.cloneNode(true);
// set the new values of the row’s fields
row.selectSingleNode("my:field1").text = "new row field 1";
row.selectSingleNode("my:field2").text = "new row field 2";
row.selectSingleNode("my:field3").text = "new row field 3";
// append the cloned row to the parent node
IXMLDOMNode parent = thisXDocument.DOM.selectSingleNode("/my:myFields");
parent.appendChild(row);

Test the form by previewing it and clicking on the button. A new row should be added to the repeating table.



User Comments

Title: .NET studio? response   
Name: Michelle
Date: 2005-05-09 7:54:05 PM
Comment:
Download the Visual Studio Toolkit for InfoPath in order to create InfoPath projects from within Visual Studio. The Toolkit adds templates allowing you to use C# or VB instead of script.

Yes, the same principle can be applied using script but the syntax would be different.
Title: .NET studio?   
Name: mark
Date: 2005-05-09 4:29:50 PM
Comment:
How do you start the project in .NET?

Also, if you create the form directly in InfoPath, can you not also do this direcly using the script editor?






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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-10-12 11:38:43 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search