List of bindings

I need an easy way to bind checkboxlist in asp.net /C# .

I pull 3 columns from database id, name and IsActive. The identifier and name, I think, will be understood by its name. And IsActive will be used to display the checked and unchecked window. I just want to know, can I bind child flags to IsActive values ​​when data binding?

eg.

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

I know the usual way of iterating over columns of elements and data, as well as comparing and checking checks. I need a simple and optimized way ...

thanks

+4
source share
2 answers

Try filling out your checkboxlist manually, I believe the code below will do your trick.

 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 ); } } 
+5
source

Unfortunately, I don't think there is a way to do this using the CheckBoxList property. Iterating through the elements seems like it could be a solution.

+1
source

All Articles