/
ListControlUtility.cs
66 lines (62 loc) · 2.32 KB
/
ListControlUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
namespace DaleWare.Web.UI.Controls
{
/// <summary>
/// This is a utility class of extension methods which can be applied to
/// a list box. It can be used outside the controls assembly for convenience
/// simply by including the namespace.
/// </summary>
public static class ListControlUtility
{
/// <summary>
/// This method returns a list of selected listitems from the listbox.
/// </summary>
/// <param name="lc">The list control.</param>
/// <returns></returns>
public static List<ListItem> SelectedItems(this ListControl lc)
{
List<ListItem> selectedItems =
lc.Items.Cast<ListItem>().Where(item => item.Selected == true).ToList();
return selectedItems;
}
#region Sort Implementation
/// <summary>
/// This method sorts the items in a listbox.
/// </summary>
/// <param name="items">The list control.</param>
/// <param name="Descending">Whether to sort the list box descending. </param>
public static void SortListItems(this ListItemCollection items, bool Descending)
{
System.Collections.Generic.List<ListItem> list = new System.Collections.Generic.List<ListItem>();
foreach (ListItem i in items)
{
list.Add(i);
}
if (Descending)
{
IEnumerable<ListItem> itemEnum =
from item in list
orderby item.Text descending
select item;
items.Clear();
items.AddRange(itemEnum.ToArray());
// anonymous delegate list.Sort(delegate(ListItem x, ListItem y) { return y.Text.CompareTo(x.Text); });
}
else
{
IEnumerable<ListItem> itemEnum =
from item in list
orderby item.Text ascending
select item;
items.Clear();
items.AddRange(itemEnum.ToArray());
//list.Sort(delegate(ListItem x, ListItem y) { return x.Text.CompareTo(y.Text); });
}
}
#endregion
}
}