Join 99,791 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,619 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
Once again I know Im missing something simple (I really need to stop working so many hours lol). I have a CheckBoxList that is populated from a SQL database:
csharp
/// <summary> /// method for populating our admin sections CheckBoxList /// </summary> private void PopulateAdminSections() { QmetroAdmin admin = new QmetroAdmin(); //get a populated DataSet to bind our CheckBoxList to DataSet sections = admin.GetAdminSectionLst();
//make sure we got data if (!(sections == null) && sections.Tables["SectionList"].Rows.Count > 0) { //set it's data source cblSectionAccess.DataSource = sections; //set it's display value cblSectionAccess.DataTextField = "SectionName"; //set it's value cblSectionAccess.DataValueField = "SectionCode"; //bind it cblSectionAccess.DataBind(); }
//now dispose of our object admin = null; }
This populates my CheckBoxList the way it is supposed to. What Im having trouble with is getting the values the user selects. Here is the method I am using to do this
csharp
/// <summary> /// method for retrieving the selected items in our CheckBoxList /// </summary> /// <returns></returns> private string GetSelectedSections() { string[] selected; //ArayList for holding the sections for the user ArrayList sections = new ArrayList();
//now loop through all the sections for (int i = 0; i < cblSectionAccess.Items.Count; i++) { if (cblSectionAccess.Items[i].Selected) { sections.Add(cblSectionAccess.Items[i].Value); } }
//now we convert the ArrayList to a pipe delimited string selected = (string[])sections.ToArray(typeof(string)); return string.Join("|", selected); }
But GetSelectedSections is always returning an empty string, even when I select items in the list. See anything glaring that I'm doing wrong here?
/// <summary> /// method for retrieving the selected items in our CheckBoxList /// </summary> /// <returns></returns> private string GetSelectedSections() { string[] selected; //ArayList for holding the sections for the user ArrayList sections = new ArrayList();
foreach (ListItem item in cblSectionAccess.Items) { if (item.Selected) { sections.Add(item.Text); } }
//now we convert the ArrayList to a pipe delimited string selected = (string[])sections.ToArray(typeof(string)); return string.Join("|", selected); }
That was how I tried it at first Jay, still saying nothing is selected. I cannot for the life of me figure out why it's not detecting that a CheckBox is checked?
That is very strange indeed. I just set-up a project using the code I gave you and it worked just fine.
CODE
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {
/// <summary> /// method for retrieving the selected items in our CheckBoxList /// </summary> /// <returns></returns> private string GetSelectedSections() { string[] selected; //ArayList for holding the sections for the user ArrayList sections = new ArrayList();
foreach (ListItem item in CheckBoxList1.Items) { if (item.Selected) { sections.Add(item.Text); } }
//now we convert the ArrayList to a pipe delimited string selected = (string[])sections.ToArray(typeof(string)); return string.Join("|", selected); } }
Hmm. I am not sure. But I see what you mean. It only seems to be a problem when bound to a DataSet. If you bind the control directly to the table it also works just fine.
I seen some examples online about binding it to a SqlDataReader, so I tried that and it still isn't working. What's the method you're referring to about binding it directly to the table? To a DataTable?