C# 以编程方式使用选中的选项填充 CheckBoxList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13740229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populating a CheckBoxList with checked options programmatically
提问by Mikey Mouse
I get the feeling I'm missing something really basic about the asp.net page life cycle here.
我觉得我在这里遗漏了有关 asp.net 页面生命周期的一些非常基本的东西。
I have a CheckBoxList (Although the same thing happens with a ListBox)
我有一个 CheckBoxList(虽然 ListBox 也会发生同样的事情)
<asp:CheckBoxList runat="server" ID="cblCountriesAccessible" DataTextField="Text" />
I'm adding a bunch of ListItems to it
我正在向它添加一堆 ListItems
var lbData = new List<ListItem>();
lbData.Add(new ListItem("x", "", true));
lbData.Add(new ListItem("y", "", false));
Then Binding them to the CheckBoxList
然后将它们绑定到 CheckBoxList
cblCountriesAccessible.DataSource = lbData;
cblCountriesAccessible.DataBind();
And every time the items in the list show up at unchecked (or unselected in a ListBox) If I add the ListItems directly to the control in the markup, they show up just fine as selected or unselected.
并且每次列表中的项目显示为未选中(或在 ListBox 中未选中)如果我将 ListItems 直接添加到标记中的控件,它们显示为选中或未选中就好了。
<asp:ListItem Text="x" Value="" Selected="True" />
What am I doing wrong in the code that selected doesn't carry through? I've tried binding CheckBoxes in with Checked set to true, and the same thing happens.
我在选择的代码中做错了什么没有执行?我试过在 Checked 设置为 true 的情况下绑定 CheckBoxes,并且发生了同样的事情。
Edit: OK, I wasn't setting Selected correctly. I'm now using
编辑:好的,我没有正确设置 Selected。我现在正在使用
var lbData = new List<ListItem>();
var l1 = new ListItem("yy", "", true) {Selected = true};
var l2 = new ListItem("xx", "", true) {Selected = false};
lbData.Add(l1);
lbData.Add(l2);
And they're still not loading as checked
他们仍然没有按检查加载
采纳答案by Adil
You can create ListItemchange its Selectedproperty and add in collection.
您可以创建ListItem更改其Selected属性并添加到集合中。
ListItem li1 = new ListItem("x", "", true);
ListItem li2 = new ListItem("x", "", true);
li1.Selected = true;
li2.Selected = true;
cblCountriesAccessible.Items.Add(li1);
cblCountriesAccessible.Items.Add(li2);
回答by Origin
ListItem.Enabledis not the same as ListItem.Selected. Per this MSDN page ( http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitem.enabled.aspx)
ListItem.Enabled不一样ListItem.Selected。每个 MSDN 页面(http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitem.enabled.aspx)
Enabled simply determines if it COULD be selected. When creating an item with this overload, it does not select it.
启用只是确定是否可以选择它。使用此重载创建项目时,它不会选择它。
Per this page ( http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx) : "To determine the selected items in the CheckBoxList control, iterate through the Items collection and test the Selected property of each item in the collection."
根据此页面(http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx):“要确定 CheckBoxList 控件中的选定项,请遍历 Items 集合并测试集合中每个项目的 Selected 属性。”
I have done this in the past using LINQ by having my list of items pre-determined and then iterating through them.
过去,我使用 LINQ 通过预先确定我的项目列表然后遍历它们来完成此操作。
Here's some un-tested code that should give you an idea. This is basically how I did this same thing with Win Forms, so your usage may be slightly different:
这是一些未经测试的代码,应该可以给你一个想法。这基本上就是我用 Win Forms 做同样的事情的方式,所以你的用法可能会略有不同:
For each item in Items
CheckedListBox.Items.Add(item.Name,Item.value,True)
Next
dim selectedItems as list(of Item) = items.where(function (x) x.selected = True)
for each item in CheckedListBox.items
item.Selected = selectedItems.contains(item.Value)
next
回答by Santoshkumar
public string GetListItems()
{
System.Collections.Generic.List<string> items = new System.Collections.Generic.List<string>();
foreach (ListItem item in lstfunction.Items)
{
if (item.Selected)
{
items.Add(item.Value);
}
}
return String.Join(", ", items.ToArray());
}

