CheckBoxList '.Selected' is returning false in every situation

asp.net, checkboxlist

Solution

I'm going to take a guess here and say that your code that does the population stuff is being called in the pageload event, so you have something like the following.

private void Page_Load()
{
    Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
    HelpingOthersEntities helpData = new HelpingOthersEntities();
    List<LookupStoreLocationsByUserName> theDataSet = helpData.LookupStoreLocationsByUserName(userGuid).ToList<LookupStoreLocationsByUserName>();
    locCkBox.DataSource = theDataSet;
    locCkBox.DataTextField = "slAddress";
    locCkBox.DataValueField = "storeLocationID";
    locCkBox.DataBind();
}

If that's the case then your effectively writing over the postback data on each request. To sort this out you need to only perform the databinding when its not a postback, so you need to change the above code to

private void Page_Load()
{
    if (!IsPostBack)
    {
        Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey;
        HelpingOthersEntities helpData = new HelpingOthersEntities();
        List<LookupStoreLocationsByUserName> theDataSet = helpData.LookupStoreLocationsByUserName(userGuid).ToList<LookupStoreLocationsByUserName>();
        locCkBox.DataSource = theDataSet;
        locCkBox.DataTextField = "slAddress";
        locCkBox.DataValueField = "storeLocationID";
        locCkBox.DataBind();
    }
}

You should then be able to test the Selected property of the items. I'd also probably change the code your using to test for the selected to something like

List<int> locList = new List<int>();
foreach(var item in locCkBox.Items)
{
  if(item.Selected)
  {
    locList.Add(int.Parse(item.Value));
  }
}

or if your on a .NET version with LINQ available

List<int> locList = new List<int>();
(from item in locCkBox.Items where item.Selected == true select item).ForEach(i => locList.Add(i.Value));

Problem

I am trying to grab multiple values from a checkboxlist and add them to a list, But checked values are always false even though the list contains appropriate count value. code to populate: ``` Guid userGuid = (Guid)Membership.GetUser().ProviderUserKey; HelpingOthersEntities helpData = new HelpingOthersEntities(); List<LookupStoreLocationsByUserName> theDataSet = helpData.LookupStoreLocationsByUserName(userGuid).ToList<LookupStoreLocationsByUserName>(); locCkBox.DataSource = theDataSet; locCkBox.DataTextField = "slAddress"; locCkBox.DataValueField = "storeLocationID"; locCkBox.DataBind(); ``` code for adding to list: ``` List<int> locList = new List<int>(); for (int x = 0; x < locCkBox.Items.Count; x++){ if(locCkBox.Items[x].Selected){ locList.Add(int.Parse(locCkBox.Items[x].Value)); } } ``` The problem I am having is that I cannot get into `items.selected` my value is always false. I have tried populating the checkboxes from postback but i get the same result. My list gives me the appropriate `.Count` amount of values, but `items.selected` = false? I have tried a foreach loop to add to list as well but I get the same results over and over. Am i missing an event or something?

Original source