Binding Checkboxlist

你离开我真会死。 提交于 2019-12-12 09:45:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!