How to set checked property of CheckBoxList elements in aspx markup?

I have a CheckBoxList on my page with the DataTextField and DataValueField attributes set, is there an attribute that I can use to specify a property that indicates whether to check it?

I hope only to set the data source and not have any code to set the checked property. Is it possible?

+4
source share
2 answers

No, this is not possible because the control uses the same binding as other controls, such as ListBox, DropDownList, RadioButtonList, etc.

According to MSDN:

To set multiple options in the program control list, a collection of controls and a set of selected properties for each individual item.

You can implement OnDataBinding for CheckListBox and then search for each element that is bound, but it may just be easier to do it all in one place.

+3
source

block in this example:

string[] strUserRoles = Roles.GetRolesForUser("Ali"); foreach (var item in Roles.GetAllRoles()) { chkRoleList.Items.Add(new ListItem() { Text = item, Value = item, Selected = strUserRoles.Contains(item) }); } 

Note: when binding CheckListBox you must set both text and value for each element.

0
source

Source: https://habr.com/ru/post/1302404/


All Articles