Binding Checkboxlist
Asked Answered
A

2

5

I need a simple way to bind the checkboxlist in asp.net /C#.

I am pulling 3 columns from database Id, Name and IsActive. Id and Name i think will be clear by its name. And IsActive will be used to show checked and unchecked box. I just want to know, can I bind the child check box values with IsActive while data binding?

E.g.

cbxlFeatures.DataSource = dt;
cbxlFeatures.DataValueField = "Id";
cbxlFeatures.DataTextField = "Name"; // something similar to this
cbxlFeatures.SomePropert= "IsActive";
cbxlFeatures.DataBind();

I know the conventional way to iterate through the items and data columns and compare and put checks. I need some easy and optimized way...

Thanks

Aindrea answered 2/8, 2012 at 9:53 Comment(1)
@humpty dumpty : thanks for formattingAindrea
F
6

Try manually populating your checkboxlist instead, I believe the code below would do the trick for you.

private void PopulateCheckBoxList( List<MyClass> myClassList )
{
    foreach ( MyClass m in myClassList )
    {
        ListItem item = new ListItem( m.Name, m.Id.ToString() );
        item.Selected = m.IsActive;
        cbxlFeatures.Items.Add( item );
    }
}
Farflung answered 2/8, 2012 at 16:5 Comment(0)
B
1

Unfortunately I don't think there is a way of doing this with a property of CheckBoxList. Iterating through the items seems that it may be the solution.

Beery answered 2/8, 2012 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.