绑定复选框列表 [英] Binding Checkboxlist
问题描述
我需要一个简单的方法来绑定 asp.net / C#
中的复选框。
I need a simple way to bind the checkboxlist in asp.net /C#
.
我正在拉来自数据库Id,Name和IsActive的3列。 Id和Name我认为它的名字将被清楚。而IsActive将用于显示已选中和未选中的框。我只是想知道,我可以绑定孩子复选框值与IsActive数据绑定吗?
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?
例如。
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...
谢谢
推荐答案
尝试手动填充你的复选框,我相信下面的代码会为你做点窍门。
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 );
}
}
这篇关于绑定复选框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!