Infopath form hosting code example

Source: Internet
Author: User
Some time ago, a form with multiple views in the project must use code to complete some special functions. The information on the Internet is not easy to find and can be sorted out. Note: The infopath encoding method is special. Controls are bound to data sources, and data sources are modified instead of control attributes.
1. Obtain non-primary data sources
Xpathnavigator mynavigator = This. datasources ["secondarydatasource"]. createnavigator ();
MessageBox. Show (mynavigator. outerxml. tostring (), "secondary data source XML ");

2. obtain information about the current view.
If (this. currentview. viewinfo. Name = "viewname") // you can specify the name of the current view.
{}
In the public void formevents_viewswitched (Object sender, viewswitchedeventargs e) event, you can use the above judgment to operate a specific view.

3. Get/set the value of a data source,
Xpathnavigator root = This. maindatasource. createnavigator ();
Xpathnavigator namefield = root. selectsinglenode ("/My: myfields/My: mainname/My: Name", namespacemanager );
Namefield. setvalue ("hello ");
String>
4. traverse the repeat control to obtain user input data
String message = "";
Xpathnavigator root = maindatasource. createnavigator ();
Xpathnodeiterator namenodes = root. Select ("/My: myfields/My: mainname/My: Name", namespacemanager );
While (namenodes. movenext ())
{
Message + = namenodes. Current. Value + system. environment. newline;
}
MessageBox. Show (Message );

5. dynamically add controls
Currentview. executeaction (actiontype. xcollectioninsert, "group_1 ");

6. Make the text value corresponding to the data source in the selected state
Xpathnavigator textnode = createnavigator (). selectsinglenode ("/My: myfields/My: field1", namespacemanager );
Currentview. selecttext (textnode );
Currentview. executeaction (actiontype. Cut );

7. Call WebService
// Open connection.
Webserviceconnection WSC = (webserviceconnection) This. dataconnections ["Main query"];
// Create xmldocuments.
Xmldocument inputdocument = new xmldocument ();
Xmldocument outputdocument = new xmldocument ();
Xmldocument errorsdocument = new xmldocument ();
// Load input document.
Inputdocument. loadxml ("<inputvalue1> test </inputvalue1> <inputvalue2> 5 </inputvalue2> ");
// Create xpathnavigator objects for documents.
Xpathnavigator inputnav = inputdocument. createnavigator ();
Xpathnavigator outputnav = outputdocument. createnavigator ();
Xpathnavigator errorsnav = errorsdocument. createnavigator ();
// Call execute method.
WSC. Execute (inputnav, outputnav, errorsnav );

When retrieving data in the repeat control, I use dataset for storage, because I found that the values of the subcontrol under each repeat cannot be traversed. For example. The repeat datatable has three subcontrols, And the Textbox Control has three values. The corresponding textbox value cannot be obtained when the datatable is traversed through the xpathnodeiterator instance. By default, the value is obtained for the first time. Therefore, we have to use datarow to traverse different columns and assign values respectively. I don't know who has a better method in the garden. Please let me know. Thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.