gpt4 book ai didi

c# - CheckBoxList 绑定(bind)需要第二个集合来确定选中状态

转载 作者:太空宇宙 更新时间:2023-11-03 22:19:02 24 4
gpt4 key购买 nike

我有一种情况需要将字符串数组数据绑定(bind)到 CheckBoxList。是否应检查每个项目的决定需要使用不同的字符串数组来完成。这是一个代码示例:

string[] supportedTransports = ... ;// "sms,tcp,http,direct"
string[] transports = ... ; // subset of the above, i.e. "sms,http"
// bind supportedTransports to the CheckBoxList
TransportsCheckBoxList.DataSource = supportedTransports;
TransportsCheckBoxList.DataBind();

这很好地绑定(bind),但每个项目都未选中。我需要以某种方式查询 transports 以确定检查状态。我想知道是否有一种简单的方法可以使用 CheckBoxList 执行此操作,或者我是否必须创建某种适配器并绑定(bind)到它?

提前致谢!

最佳答案

您可以为此使用一些 LINQ:

        string[] supportedTransports = { "sms", "tcp", "http", "direct" };
string[] transports = { "sms", "http" };

CheckBoxList1.DataSource = supportedTransports;
CheckBoxList1.DataBind();

foreach (ListItem item in CheckBoxList1.Items)
{
if (transports.Contains(item.Text))
{
item.Selected = true;
}
}

关于c# - CheckBoxList 绑定(bind)需要第二个集合来确定选中状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3906329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com