Solved

pre-selecting checkboxlist items not working

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
grab and show facebook pages event calendars 6 65
Hidden Field Value 10 63
How to LOAD up a .NET Framework website with data? 2 42
ASP.NET 5 Templates 2 68
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now