Link to home
Start Free TrialLog in
Avatar of mtadj
mtadj

asked on

pre-selecting checkboxlist items not working

We have a checkboxlist control that displays a list of checkboxes.   We need to have all of these checked when the form loads.  We've written code to do this, but when the page loads, all the checkboxes remain unchecked.  Below is the relevant code, any ideas?

This is the control on our .ascx file:
<asp:CheckBoxList id="cblTypeOfDiabetes" runat="server" RepeatLayout="Flow" RepeatDirection="Vertical" DataTextField="Text" DataValueField="Value"  />


This is in the code-behind, .cs file.  Everything works, like checkboxes have proper text and values, but they are not pre-selected.

      // create DataTable to store value/text for each checkbox
      cblTable = new DataTable("Diabetes");
      cblTable.Columns.Add(new DataColumn("Value"));
      cblTable.Columns.Add(new DataColumn("Text"));

      // get data from content management system.
      Item diabetesFolder = Sitecore.Context.Database.Items["/sitecore/content/Reference/Diabetes"];
      Item[] diabetesItems = diabetesFolder.Axes.SelectItems("./*[@@templatename='Checklist Item']");

      // populate DataTable
      foreach (Item diabetesItem in diabetesItems)
      {
        DataRow row = cblTable.NewRow();
        row["Value"] = diabetesItem.ID.ToString();
        row["Text"] = diabetesItem.Fields["label"].Value;
        cblTable.Rows.Add(row);
      }

      // bind datasource to checkboxlist
      cblTypeOfDiabetes.DataSource = cblTable;
      cblTypeOfDiabetes.DataTextField = "Text";
      cblTypeOfDiabetes.DataValueField = "Value";
      cblTypeOfDiabetes.DataBind();

      // pre-select each item.  NOTE: when checkboxes are rendered on the web page, they are still not selected??
      foreach (ListItem li in cblTypeOfDiabetes.Items)
      {
        li.Selected = true;
      }
ASKER CERTIFIED SOLUTION
Avatar of strickdd
strickdd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mtadj
mtadj

ASKER

true genius.  Thanks so much!