0

I'm having a bit of trouble with my CheckBoxList control in ASP.NET - it seems to not store the values that I have checked (?)

This is the test-code I have at the moment:

for (int i = 0; i < cbMemberTypes.Items.Count; i++)
{
   if (cbMemberTypes.Items[i].Selected)
   {
      // do stuff
   }
}

I have tried to output the amount of checked items, but it is always returning 0. I'm rather lost here, because in my other usercontrol, it works perfectly! Any hints on this will be greatly appreciated! :-)

1 Answer 1

3

Ditto @Shiraz: check to see if you are binding or setting values in the Page_Load.

Since Page_Load fires before the button click event handler, the control is being rebound each time you click and the users' selection is wiped out. Try wrap it in a if(!IsPostBack) { /* bind CBLlist */ }.

Sign up to request clarification or add additional context in comments.

4 Comments

Hey Brad, yes i'm binding in the Page_Load event :) Else the values of the checkboxlist wouldn't show up. I'm binding like this: cbMemberTypes.DataSource = foedeVareNode.Children; cbMemberTypes.DataTextField = "Name"; cbMemberTypes.DataBind();
Sadly I can't debug it because it's a usercontrol i'm using in Umbraco CMS :(
@bomortensen, it's definitely a life-cycle issue; check my edits.
Brad, thanks a lot! I honestly don't know why I didn't see that! That was one those "facepalm" moments ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.