I get the feeling I'm missing something really basic about the asp.net page life cycle here.
I have a CheckBoxList (Although the same thing happens with a ListBox)
<asp:CheckBoxList runat="server" ID="cblCountriesAccessible" DataTextField="Text" />
I'm adding a bunch of ListItems to it
var lbData = new List<ListItem>();
lbData.Add(new ListItem("x", "", true));
lbData.Add(new ListItem("y", "", false));
Then Binding them to the CheckBoxList
cblCountriesAccessible.DataSource = lbData;
cblCountriesAccessible.DataBind();
And every time the items in the list show up at unchecked (or unselected in a ListBox) If I add the ListItems directly to the control in the markup, they show up just fine as selected or unselected.
<asp:ListItem Text="x" Value="" Selected="True" />
What am I doing wrong in the code that selected doesn't carry through? I've tried binding CheckBoxes in with Checked set to true, and the same thing happens.
Edit: OK, I wasn't setting Selected correctly. I'm now using
var lbData = new List<ListItem>();
var l1 = new ListItem("yy", "", true) {Selected = true};
var l2 = new ListItem("xx", "", true) {Selected = false};
lbData.Add(l1);
lbData.Add(l2);
And they're still not loading as checked