Solve the issue of sorting infopath level-2 Association

Source: Internet
Author: User

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]);
}
}
}

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.