如何按字母顺序排列两个不同 CheckBoxList 中的项目?

发布于 2024-08-25 10:13:37 字数 76 浏览 1 评论 0原文

我有两个复选框(推荐和其他),其中包含人员姓名(串联,即约翰·史密斯是一项)。我想将每个列表中选定的成员按字母顺序排列为一个。我该怎么做?

I have two checkboxes (Recommended and Others) which have peoples names (concatenated, i.e. John Smith is one item). I want to alphabetize the selected members of each list into one. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄与心 2024-09-01 10:13:37

具有三个复选框列表控件(chkRecommended、chkOthers、chkCombined)的 ASP.NET 实现

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

如果您只是指值列表而不是另一个控件,则可以修改我提供的原始查询或像这样扩展它

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

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);
妄想挽回 2024-09-01 10:13:37

您可以将两者的选定成员放入字符串列表中并排序,然后将字符串列表放入新的 CheckBoxList 中。

请参阅 http://msdn.microsoft.com/en-us/library/b0zbh7b6 .aspx 用于 MSDN List(T).Sort Method 示例。

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.

你又不是我 2024-09-01 10:13:37

如果您从 SqlDataSource 获取它们,为什么不通过 SQL 按字母顺序排列呢?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文