0
votes

There is CheckBoxList with items:

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

When it is bind in C# code:

BitArray ba = new BitArray(3);
ba[0] = false;
ba[1] = true;
ba[2] = false;
cbl.DataSource = ba;
cbl.DataBind();

I expected: to see just marked checkboxes with no text

but instead the result is: Boolean values went to label texts

I no need a label text. Just need to set checkbox mark by Boolean value from BitArray item. If it is matter there are about 600 CheckBoxList controls with about 20 checkboxes in each. So making separate class would slow down performance of web page. What is the property name of BitArray items to bind it CheckBoxList or how efficiently bind it?

Edit: Thanks to combatc2 by setting

cb1.DataTextFormatString =  " ";

I get rid of label texts but checkboxes still are not correctly set. Instead of correctly setting "checked" property:

<input name="cbl3$1" id="cbl3_1" type="checkbox" value="">
<input name="cbl3$0" id="cbl3_0" type="checkbox" checked="checked" value="">

it is wrongly set "value" property:

<input name="cbl2$0" id="cbl2_0" type="checkbox" value="False">
<input name="cbl2$1" id="cbl2_1" type="checkbox" value="True">
2

2 Answers

1
votes

Add this line of code prior to calling DataBind():

cb1.DataTextFormatString =  " ";

They should be - if you loop thru the items (say on a button click) you should see that the values are correctly set:

protected void OnClick(object sender, EventArgs e)
{
    foreach (ListItem item in cb1.Items)
    {
        var result = bool.Parse(item.Value);    
        System.Diagnostics.Debug.WriteLine(result);            
    }
}
0
votes

You are only binding data. If you want to set the values also you're gonna have to loop the values. You could use a method that returns a complete RadioButtonList. That way you don't have to loop each list separate.

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