如何用代码对repeating section控件新增Item(InfoPath)
在做项目的时候,有一个场景,当用户切换不同选项时(radio button),repeating section会随着切换而变换两种状态1:删除所有item. 2.新增而且只新增一个item.
对于删除比较容易,但对于从没有任何一个item状态下,新增一个item,来讲,在网上找了很久,也没有找到合适的解决办法。经过几次试验,终于有了一个比较理想的解决方案。即用临时变量存放section item的xml。然后当状态切换时,用代码AppendChild(xml)来动态新增一个item.
infopath form的dataSource如下结构:
-myFields
|-group1
|-group2
|-field1
|-field2
删除:
XPathNavigator domNav = MainDataSource.CreateNavigator();
XPathNodeIterator it = domNav.Select("/my:myFields/my:group1/my:group2", NamespaceManager);
while (it.Count > 0)
{
it.MoveNext();
it.Current.DeleteSelf();
it = domNav.Select("/my:myFields/my:group1/my:group2", NamespaceManager);
}
新增 :
1),用临时变量存放section item的xml. 注意:如果是web based的,要用session来存放这个变量
private object _NodeXml
{
get
{
return FormState["_nodeXml"];
}
set
{
FormState["_nodeXml"] = value;
}
}
public void FormsEvents_Loading(object sender,LoadingEventArgs e)
{
XPathNavigator pare = domNav.SelectSingleNode("/my:myFields/my:group1/my:group2", NamespaceManager);
if (pare != null)
{
_NodeXml= pare.OuterXml;
}
}
2)在另外的事件下面:
XPathNavigator domNav = MainDataSource.CreateNavigator();
XPathNavigator pare = domNav.SelectSingleNode("/my:myFields/my:group1", NamespaceManager);
pare.AppendChild(_NodeXml);