开发者

How do I alphabetize the items in two different CheckBoxLists?

I have two checkboxes (Recommended and Others) which have peoples names (concatenated, i.e. John Smith is o开发者_运维知识库ne item). I want to alphabetize the selected members of each list into one. How can I do this?


An ASP.NET implementation with three checkboxlist controls (chkRecommended, chkOthers, chkCombined)

var listItems = (from ListItem listItem in chkRecommended.Items
                 where listItem.Selected
                 select listItem)
                .Union(from ListItem listItem in chkOthers.Items
                       where listItem.Selected
                       select listItem)
                .OrderBy(listItem => listItem.Text);

chkCombined.Items.Clear();
foreach (ListItem listItem in listItems)
    chkCombined.Items.Add(listItem);

If you just meant a list of the values rather than another control, you can modify the original query I provided or extend it like so

var listValues = listItems.Select(listItem => listItem.Value);


You can put the selected members of both into a List of strings and sort, then put the list of strings into a new CheckBoxList.

See http://msdn.microsoft.com/en-us/library/b0zbh7b6.aspx for the MSDN List(T).Sort Method example.


If you're grabbing them from a SqlDataSource, why not do the alphabetizing via SQL?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜