Solved

pre-selecting checkboxlist items not working

Posted on 2006-11-09
2
1,043 Views
Last Modified: 2012-06-27
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;
      }
0
Comment
Question by:mtadj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 17906018
Try this, add it under (not in) the PageLoad function


protected void Page_PreRender(object sender, EventArgs e)
    {
          / 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;
      }                                
    }
0
 

Author Comment

by:mtadj
ID: 17906162
true genius.  Thanks so much!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question