Welcome to Dream.In.Code
Getting Help is Easy!

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!



Get selected values from CheckBoxList

2 Pages V  1 2 >  
Reply to this topicStart new topic

Get selected values from CheckBoxList

PsychoCoder
post 30 Apr, 2008 - 05:31 PM
Post #1


using Coding.God;

Group Icon
Joined: 26 Jul, 2007
Posts: 6,655



Thanked 26 times

Dream Kudos: 7350

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#

My Contributions


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?
User is offlineProfile CardPM

Go to the top of the page


PsychoCoder
post 30 Apr, 2008 - 10:31 PM
Post #2


using Coding.God;

Group Icon
Joined: 26 Jul, 2007
Posts: 6,655



Thanked 26 times

Dream Kudos: 7350

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#

My Contributions


Now I'll be impatient like others are everyday LOL tongue.gif

Anyone got any ideas for me?
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 30 Apr, 2008 - 11:12 PM
Post #3


using Coding.God;

Group Icon
Joined: 26 Jul, 2007
Posts: 6,655



Thanked 26 times

Dream Kudos: 7350

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#

My Contributions


Using Response.Write() throughout the GetSelectedSections() method I know that this code

csharp

if (cblSectionAccess.Items[i].Selected)
{
sections.Add(cblSectionAccess.Items[i].Value);
}


Isnt being execute. I can select each CheckBox in the list and they still say False to being selected.
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 1 May, 2008 - 05:50 AM
Post #4


using Coding.God;

Group Icon
Joined: 26 Jul, 2007
Posts: 6,655



Thanked 26 times

Dream Kudos: 7350

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#

My Contributions


This is getting frustrating, no matter what I try or do I cant get to this line of code


csharp

sections.Add(cblSectionAccess.Items[i].Value);


As it always returns false when checking if an item is selected. Has anyone else ever ran into this situation? If so, how did you solve it?
User is offlineProfile CardPM

Go to the top of the page

jayman9
post 1 May, 2008 - 09:04 AM
Post #5


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,086



Thanked 14 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


This is how I do it.

Example:
CODE

/// <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);
}
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 1 May, 2008 - 10:36 AM
Post #6


using Coding.God;

Group Icon
Joined: 26 Jul, 2007
Posts: 6,655



Thanked 26 times

Dream Kudos: 7350

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#

My Contributions


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?
User is offlineProfile CardPM

Go to the top of the page

jayman9
post 1 May, 2008 - 10:45 AM
Post #7


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,086



Thanked 14 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


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)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("Checkboxes selected = " + GetSelectedSections());
    }


    /// <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);
    }
}


Attached thumbnail(s)
Attached Image
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 1 May, 2008 - 10:55 AM
Post #8


using Coding.God;

Group Icon
Joined: 26 Jul, 2007
Posts: 6,655



Thanked 26 times

Dream Kudos: 7350

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#

My Contributions


Now I'm truly confused, heres a screenshot of what I get when I use the same exact code

Attached Image


Could it be because I'm populating the CheckBoxList from a database?
User is offlineProfile CardPM

Go to the top of the page

jayman9
post 1 May, 2008 - 11:16 AM
Post #9


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,086



Thanked 14 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


Populating it how? Is it DataBound to a data source?
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 1 May, 2008 - 11:19 AM
Post #10


using Coding.God;

Group Icon
Joined: 26 Jul, 2007
Posts: 6,655



Thanked 26 times

Dream Kudos: 7350

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#

My Contributions


Yes, it's bound to a DataSet

Well I populated it manually and it works fine. Any reason why it doesn't work when it's data bound?

This post has been edited by PsychoCoder: 1 May, 2008 - 11:22 AM
User is offlineProfile CardPM

Go to the top of the page

jayman9
post 1 May, 2008 - 11:50 AM
Post #11


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,086



Thanked 14 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


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.

Sorry I can't be of more help.
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 1 May, 2008 - 11:52 AM
Post #12


using Coding.God;

Group Icon
Joined: 26 Jul, 2007
Posts: 6,655



Thanked 26 times

Dream Kudos: 7350

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#

My Contributions


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?
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 7/25/08 01:53AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->