If you use a Sql Source to populate the check list (As shown here: http://msdn.microsoft.com/en-us/library/heyd79hc.aspx), trying to change the status of a check item may not work during a Page_Load event (for example, trying to change what is checked in your list). This is likely because the checklist has not finished databinding to its source.
What worked for me is, adding an event handler to the OnDataBound event of the check box list.
The other option is probably to call this.Databind(); or CheckBoxList2.Databind(); in the Page_Load; then, doing the check/uncheck logic. I did not try that however.
<asp:CheckBoxListID="CheckBoxList2"runat="server"DataSourceID="SqlDataSource1"DataTextField="Name"DataValueField="pkid"RepeatDirection="Horizontal"CellPadding="1"CellSpacing="3"RepeatColumns="4"OnDataBound="CheckBox2_Bound">
</asp:CheckBoxList>
Then, in your code behind, running something similar to what is below to initialize your check list with the correct selected items:
protected void CheckBox2_Bound(object sender, EventArgs e) {
CheckIt(thisUsr.AllowedList, CheckBoxList2);
}
private void CheckIt(string AllowedList, CheckBoxList CheckList) {
System.Diagnostics.Debug.WriteLine("Checking " + CheckList.ID);
for (int i = 0; i < CheckList.Items.Count; i++) {
System.Diagnostics.Debug.WriteLine("Checking if user has " + CheckList.Items[i].Value);
if (AllowedList.Contains(CheckList.Items[i].Value)) {
CheckList.Items[i].Selected = true;
System.Diagnostics.Debug.WriteLine("Yes");
} else { System.Diagnostics.Debug.WriteLine("No"); }
}
}
Cheers,