In the dropdown of infopath association, we encounter the issue that dropdown cannot be sorted. let's talk about the infopath linkage field. A colleague quoted http://www.msotec.com/showtopic-1930.aspxcode. solved the pressing problem at that time. however, when users used the form later, they found that the form was very slow. at first, I didn't pay attention to this because we made a lot of rules on the form. this causes performance degradation.
When I opened the linkage code today, I found that many areas can be optimized. The following is the optimized code.
Private void actionreasondatabind (string action)
{
If (Action = string. Empty) return;
// Get reason and backreason
Xpathnavigator reasiondatasource = This. datasources ["reason"]. createnavigator ();
Xpathnavigator backreasiondatasource = This. datasources ["backreason"]. createnavigator ();
// Filter reason based on action
String xpathstring = string. Format ("/DFS: myfields/DFS: datafields/DFS: reason [@ action = '{0}']", action );
Xpathnodeiterator newreasions = backreasiondatasource. Select (xpathstring, this. namespacemanager );
// Clear all action reason
Reasiondatasource = reasiondatasource. selectsinglenode ("/DFS: myfields/DFS: datafields ",
Namespacemanager );
Xpathnodeiterator reasions = reasiondatasource. selectchildren (xpathnodetype. element );
Reasions. Current. movetofirstchild ();
Do
{
Reasions. Current. deleteself ();
}
While (reasions. Current. movetofirstchild ());
// Copy the reason Value
If (newreasions. Count! = 0)
{
Newreasions. movenext ();
Do
{
Reasions. Current. appendchild (newreasions. Current. Clone ());
}
While (newreasions. movenext ());
}
}
As mentioned above, I have encountered sorting problems. In order to solve this problem, I went around msdn for half a day and looked for sorting about XML. we know that the data obtained through SharePoint connections is all attribute styles. for example:
<DFS: reason action = "hire" reason = "Sort value"/>.
Xpathexpression does not support attribute sorting, Which is depressing ..., I even want to use LINQ, but LINQ can only be. net Framework 3.5, and my form code is.. NET Framework 2.0, which may cause unnecessary errors. so I used the intermediate variable sortlist. filter all the xpathnavigator objects to the sortlist object first, and set the value of the reason attribute as the key. in this way, sortlist can be automatically sorted. then, assign the sorted value to reason dropdown. The Code is as follows:
Private void actionreasondatabind (string action)
{
If (Action = string. Empty) return;
// Get reason and backreason
Xpathnavigator reasiondatasource = This. datasources ["reason"]. createnavigator ();
Xpathnavigator backreasiondatasource = This. datasources ["backreason"]. createnavigator ();
// Filter reason based on action
String xpathstring = string. Format ("/DFS: myfields/DFS: datafields/DFS: reason [@ action = '{0}']", action );
Xpathnodeiterator newreasions = backreasiondatasource. Select (xpathstring, this. namespacemanager );
// Clear all action reason
Reasiondatasource = reasiondatasource. selectsinglenode ("/DFS: myfields/DFS: datafields ",
Namespacemanager );
Xpathnodeiterator reasions = reasiondatasource. selectchildren (xpathnodetype. element );
Reasions. Current. movetofirstchild ();
Do
{
Reasions. Current. deleteself ();
}
While (reasions. Current. movetofirstchild ());
// Copy the xmlelement to sortlist, In order to sort
Sortedlist slist = new sortedlist ();
If (newreasions. Count! = 0)
{
Newreasions. movenext ();
Do
{
Slist. Add (newreasions. Current. selectsinglenode ("@ reason"). Value, newreasions. Current. Clone ());
}
While (newreasions. movenext ());
}
// Add reason records into action reason dropdownlist
If (slist. Count! = 0)
{
Foreach (Object key in slist. Keys)
{
Reasions. Current. appendchild (xpathnavigator) slist [Key]);
}
}
}