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["/si tecore/con tent/Refer ence/Diabe tes"];
Item[] diabetesItems = diabetesFolder.Axes.Select Items("./* [@@templat ename='Che cklist 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.DataSour ce = cblTable;
cblTypeOfDiabetes.DataText Field = "Text";
cblTypeOfDiabetes.DataValu eField = "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;
}
This is the control on our .ascx file:
<asp:CheckBoxList id="cblTypeOfDiabetes" runat="server" RepeatLayout="Flow" RepeatDirection="Vertical"
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.
Item[] diabetesItems = diabetesFolder.Axes.Select
// populate DataTable
foreach (Item diabetesItem in diabetesItems)
{
DataRow row = cblTable.NewRow();
row["Value"] = diabetesItem.ID.ToString()
row["Text"] = diabetesItem.Fields["label
cblTable.Rows.Add(row);
}
// bind datasource to checkboxlist
cblTypeOfDiabetes.DataSour
cblTypeOfDiabetes.DataText
cblTypeOfDiabetes.DataValu
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER