Solved

pre-selecting checkboxlist items not working

Posted on 2006-11-09
2
1,042 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

Independent Software Vendors: 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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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