[ACCEPTED]-ASP.NET CheckBoxList DataBinding Question-web-controls

Accepted answer
Score: 43

EDIT: There's no way to do this through the Markup. The 6 DataValueField does not determine whether 5 the checkbox item is check or not. It retrieves 4 or stores the value to be used in postbacks. The 3 DataValueField is common across CheckBoxLists, RadioButtonLists, ListControl, etc.

This 2 is about the only way to pre-select the 1 checkboxes as you already found out.

chkListRoles.DataSource = usersInRole;
chkListRoles.DataBind();

foreach(ListItem item in chkListRoles.Items)
 item.Selected = usersInRole.Find(u => u.UserName == item.Text).InRole;
Score: 10

I made a custom control for this, after 7 getting tired of the OnItemDataBound-binding. It will let 6 you bind the Selected attribute. You can easily 5 make the same control for RadioButtonList by changing what 4 the custom control derives from.

To use this, simply 3 add the DataCheckedField attribute when you create the control 2 in your markup. Remember to reference the 1 custom controls in your web.config file.

Markup

<myControls:SimpleCheckBoxList runat="server" ID="chkListRoles"
                               DataCheckedField="InRole"
                               DataTextField="UserName"
                               DataValueField="UserId" />

Code for the control

public class SimpleCheckBoxList : System.Web.UI.WebControls.CheckBoxList
{
    public string DataCheckedField
    {
        get
        {
            string s = (string)ViewState["DataCheckedField"];
            return (s == null) ? String.Empty : s;
        }
        set
        {
            ViewState["DataCheckedField"] = value;
            if (Initialized)
                OnDataPropertyChanged();
        }
    }

    protected override void PerformDataBinding(IEnumerable dataSource)
    {
        if (dataSource != null)
        {
            if (!this.AppendDataBoundItems)
                this.Items.Clear();

            if (dataSource is ICollection)
                this.Items.Capacity = (dataSource as ICollection).Count + this.Items.Count;

            foreach (object dataItem in dataSource)
            {
                ListItem item = new ListItem()
                {
                    Text = DataBinder.GetPropertyValue(dataItem, DataTextField).ToString(),
                    Value = DataBinder.GetPropertyValue(dataItem, DataValueField).ToString(),
                    Selected = (DataCheckedField.Length > 0) ? (bool)DataBinder.GetPropertyValue(dataItem, DataCheckedField) : false
                };
                this.Items.Add(item);
            }
        }
    }
}
Score: 6

It's not possible using markup. What you 9 can do is to bind the checkboxlist like 8 you wanted it to work - with the bool in 7 the DataValueField, and then simply add 6 this as OnDataBound event.

protected void myCheckBoxList_DataBound(object sender, EventArgs e)
    {
        foreach (ListItem item in myCheckBoxList.Items)
        {
            item.Selected = bool.Parse(item.Value);
        }
    }

The difference 5 between this solution and the one proposed 4 by Jose Basilio is that this one works with 3 all kind of databinding methods. For example 2 binding with a SelectMethod using the new 1 ModelBinding feature in v4.5.

Score: 1

Using a DataList could be another option

<asp:DataList ID="dataListRoles" runat="server">
    <ItemTemplate>
        <asp:CheckBox runat="server" Text='<%# Eval("UserName ") %>' Checked='<%# Eval("IsInRole") %>' />
    </ItemTemplate>
</asp:DataList>

0

Score: 0

I would think you would have to tell the 8 control what property to bind it to...in 7 this case "InRole".

I played around with 6 it and seems like there is noway to bind 5 to the selection of the checkbox, you have 4 to do it yourself. I was able to bind to 3 the text and values of the checklist which 2 only seem to deal with the label of each 1 checkbox in the list.

More Related questions