问题
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
回答1:
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 );
}
}
回答2:
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.
来源:https://stackoverflow.com/questions/11775044/binding-checkboxlist