Solved

pre-selecting checkboxlist items not working

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

9 Experts available now in Live!

Get 1:1 Help Now