InfoPath is a powerful tool which can be customized by using rules or custom code behind. At one of my projects it was necessary to display some additional information from a database dependent on which checkbox the user clicked / checked. Another request to this was that the form has to be browser-enabled cause it should be displayed in a SharePoint environment.
So i have 3 Checkboxes: A, B, C. And i have a section with a repeating table which is structured like this:
RepeatingTable
—–FieldA
—–FieldB
—–FieldC
—–FieldD
—–FieldE
I use the onchange Event of each Checkbox which calls the function. Now let’s see how we can fill this repeating table with data:
[sourcecode language=”csharp”]
XPathNavigator node = navForm.SelectSingleNode(“/my:meineFelder/my:Section/my:RepeatingTable”, NS).Clone();
foreach (DataRow row in DataSet.Tables[0].Rows)
{
node.SelectSingleNode(“/my:meineFelder/my:Section/my:RepeatingTable/my:FieldA”, NS).SetValue(row[“Field1”].ToString());
node.SelectSingleNode(“/my:meineFelder/my:Section/my:RepeatingTable/my:FieldB”, NS).SetValue(row[“Field2”].ToString());
node.SelectSingleNode(“/my:meineFelder/my:Section/my:RepeatingTable/my:FieldC”, NS).SetValue(row[“Field3”].ToString());
node.SelectSingleNode(“/my:meineFelder/my:Section/my:RepeatingTable/my:FieldD”, NS).SetValue(row[“Field4”].ToString());
node.SelectSingleNode(“/my:meineFelder/my:Section/my:RepeatingTable/my:FieldE”, NS).SetValue(row[“Field5”].ToString());
navForm.SelectSingleNode(“/my:meineFelder/my:Section”, NS).AppendChild(node);
}
node.DeleteSelf();
[/sourcecode]
At first, i clone the node. After that i fill the fields of this clone with my data of my dataset. Now i can append this node to the current repeating table. After finishing the foreach i delete the node. After that you can see your data in the repeating table like this:
But what happens if the user clicks now on the checkbox B? Normally it appends the rows to the current repeating table. So we have to delete all rows first. For that i use a small trick.
I do not delete each row, instead i delete the whole repeating table and create a new one. In order to do that i save at formEvents Loading the empty repeating table into a member variable from which i can create a new repeating table.
So first add a member variable to your code:
[sourcecode language=”csharp”]
private object initialData
{
get
{
return FormState[“initialData”];
}
set
{
FormState[“initialData”] = value;
}
}
[/sourcecode]
After that i add in the FormEvents_Loading
[sourcecode language=”csharp”]
this.initialData = navForm.SelectSingleNode(“/my:meineFelder/my:Section”, NS).OuterXml;
[/sourcecode]
Now the interesting part begins: How to use this member variable for creating a new empty repeating table:
[sourcecode language=”csharp”]
if (initialData != null)
navForm.SelectSingleNode(“/my:meineFelder/my:Section”, NS).ReplaceSelf(initialData.ToString());
[/sourcecode]
That’s it, just use the ReplaceSelf funtion of infopath. Hope this helps you.
..:: I LIKE SHAREPOINT ::..
Leave a Reply
You must be logged in to post a comment.