C# 如何将CheckBoxList绑定到BitArray?
有包含以下项目的复选框列表:C# 如何将CheckBoxList绑定到BitArray?,c#,asp.net,binding,checkboxlist,bitarray,C#,Asp.net,Binding,Checkboxlist,Bitarray,有包含以下项目的复选框列表: <asp:CheckBoxList ID="RadCheckBoxList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"> <Items> <asp:ListItem Text="" Value="1" /> <asp:ListItem Text="" Value="2" />
<asp:CheckBoxList ID="RadCheckBoxList1" runat="server" RepeatDirection="Horizontal"
RepeatLayout="Flow">
<Items>
<asp:ListItem Text="" Value="1" />
<asp:ListItem Text="" Value="2" />
<asp:ListItem Text="" Value="3" />
</Items>
</asp:CheckBoxList>
我期望:
但结果却是:
我不需要标签文本。只需要从BitArray项中通过布尔值设置复选框标记。
如果有问题的话,大约有600个复选框列表控件,每个控件中大约有20个复选框。所以,创建单独的类会降低网页的性能。
要绑定它的BitArray项的属性名是什么?CheckBoxList,或者绑定它的效率如何
编辑:
多亏了combatc2通过设置
cb1.DataTextFormatString = " ";
我去掉了标签文本,但复选框仍然没有正确设置。不是正确设置“已检查”属性:
<input name="cbl3$1" id="cbl3_1" type="checkbox" value="">
<input name="cbl3$0" id="cbl3_0" type="checkbox" checked="checked" value="">
<input name="cbl2$0" id="cbl2_0" type="checkbox" value="False">
<input name="cbl2$1" id="cbl2_1" type="checkbox" value="True">
错误地设置了“值”属性:
<input name="cbl3$1" id="cbl3_1" type="checkbox" value="">
<input name="cbl3$0" id="cbl3_0" type="checkbox" checked="checked" value="">
<input name="cbl2$0" id="cbl2_0" type="checkbox" value="False">
<input name="cbl2$1" id="cbl2_1" type="checkbox" value="True">
在调用DataBind()之前添加这行代码: 它们应该是-如果您在项目中循环(比如单击按钮),您应该看到值设置正确:
protected void OnClick(object sender, EventArgs e)
{
foreach (ListItem item in cb1.Items)
{
var result = bool.Parse(item.Value);
System.Diagnostics.Debug.WriteLine(result);
}
}
您只是在绑定数据。如果你想设置这些值,你必须循环这些值。您可以使用返回完整RadioButtonList的方法。这样,您就不必单独循环每个列表
PlaceHolder1.Controls.Add(getCompleteRadioButtonList(ba));
public RadioButtonList getCompleteRadioButtonList(BitArray ba)
{
//create a new radiobuttonlist
RadioButtonList rbl = new RadioButtonList();
//do the binding
rbl.DataSource = ba;
rbl.DataTextFormatString = " ";
rbl.DataBind();
//loop the values to set the items that were just bound
for (int i = 0; i < ba.Count; i++)
{
rbl.Items[i].Selected = ba[i];
}
//return the list
return rbl;
}
placeholder 1.Controls.Add(getCompleteRadioButtonList(ba));
公共RadioButtonList getCompleteRadioButtonList(位数组ba)
{
//创建一个新的radiobuttonlist
RadioButtonList rbl=新的RadioButtonList();
//装订
rbl.DataSource=ba;
rbl.DataTextFormatString=“”;
rbl.DataBind();
//循环值以设置刚绑定的项
for(int i=0;i
好处是标签消失了,但复选框没有设置为BitArray中的布尔值。我看到了正确的值,但标记是错误的:我将第二个复选框列表放在标记中的复选框处:。。。在网页上正确呈现:属性“值”错误。需要设置属性“checked”。到目前为止,我使用循环来设置复选框,但我希望分配DataSource和DataBind()也能更快地完成同样的操作。这两种方法1循环和2数据绑定不能一起使用。