6 Replies - 2889 Views - Last Post: 05 August 2013 - 08:26 AM Rate Topic: -----

#1 staindromeo   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 155
  • Joined: 21-October 10

Select Certain ListItems in CheckboxList

Posted 01 August 2013 - 09:26 AM

Hey,

I have a checkboxlist populated but I am trying to select only few of the items instead of having them all selected/not selected. Here's my code:

//Populate 
            DataSet dsCBL = Verif.GC(Convert.ToInt32(Session["ID"]));
            for (int l = 0; l < dsCBL.Tables[0].Rows.Count; l++)
            {
                ListItem listItem = new ListItem();
                listItem.Value = dsCBL.Tables[0].Rows[l][0].ToString();
                listItem.Text = dsCBL.Tables[0].Rows[l][1].ToString();
                listItem.Selected = false;
             //listItem.Selected = true; (Selects all items)
                CheckBoxListTest.Items.Add(listItem);     
            }


Now I have tried CheckboxListTest.SelectedValue = "(The Item Name)" and that didn't work it still showed as all the items not selected.

Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Select Certain ListItems in CheckboxList

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: Select Certain ListItems in CheckboxList

Posted 01 August 2013 - 09:49 AM

It is because you are putting in a loop with no type of test involved. Just put in the test for the criteria for when to select/not select an item...

//Populate 
DataSet dsCBL = Verif.GC(Convert.ToInt32(Session["ID"]));
for (int l = 0; l < dsCBL.Tables[0].Rows.Count; l++)
{
                ListItem listItem = new ListItem();
                listItem.Value = dsCBL.Tables[0].Rows[l][0].ToString();
                listItem.Text = dsCBL.Tables[0].Rows[l][1].ToString();
                listItem.Selected = false;

                // Would only select the item if it matched some value
                if (listItem.Text == "Some value") { listItem.Selected = true; }
                CheckBoxListTest.Items.Add(listItem);     
}



Here we are showing that if the listItem.Text was some kind of text value, then it would select the item. Otherwise it wouldn't select it.

:)
Was This Post Helpful? 1
  • +
  • -

#3 staindromeo   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 155
  • Joined: 21-October 10

Re: Select Certain ListItems in CheckboxList

Posted 01 August 2013 - 12:30 PM

Thanks Martyr, this worked however I am having trouble setting the values to a session. I need the Selected Value but if I try a foreach loop that doesn't work. The error I get is that the datatype varchar cannot be converted to int as the text is being passed but not the SelectedValue.
Here's what I tried:

//Populate 
            DataSet dsCBL = Verif.GC(Convert.ToInt32(Session["ID"]));
            for (int l = 0; l < dsCBL.Tables[0].Rows.Count; l++)
            {
                ListItem listItem = new ListItem();
                listItem.Value = dsCBL.Tables[0].Rows[l][0].ToString();
                listItem.Text = dsCBL.Tables[0].Rows[l][1].ToString();
                listItem.Selected = false;
             //listItem.Selected = true; (Selects all items)

 if (listItem.Text == "Test1" || listItem.Text == "Test2") 
{ 
listItem.Selected = true; 
                CheckBoxListTest.Items.Add(listItem);     
            }

//Selected Value to Session
 foreach (ListItem item in CheckBoxListtest.Items)
                {
                    if (item.Selected)
                    {
                        Session.Add("SessionCBL", item);
                    }
                }


Was This Post Helpful? 0
  • +
  • -

#4 staindromeo   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 155
  • Joined: 21-October 10

Re: Select Certain ListItems in CheckboxList

Posted 01 August 2013 - 04:05 PM

Alright I got the selected values in a string as 1,2,3,4,5,6 but how do iterate over the string so that all the multiple strings are in the session variable as an int. here's my code :
string values = "";
                for (int i = 0; i < .CheckboxlistTest.Items.Count; i++)
                {
                    if (CheckboxlistTest.Items[i].Selected)
                    {
                        values += CheckboxlistTest.Items[i].Value + ",";
                    }
                }
                values = values.TrimEnd(',');
                for (int a = 0; a < values.Length; a++)
                {
                    Session.Add("SessionCBL", values[a]);
                }

This post has been edited by staindromeo: 01 August 2013 - 04:06 PM

Was This Post Helpful? 0
  • +
  • -

#5 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5552
  • View blog
  • Posts: 14,558
  • Joined: 18-April 07

Re: Select Certain ListItems in CheckboxList

Posted 01 August 2013 - 04:30 PM

I think you are making this much more complicated that it has to be. If you are looping through items to add them to a checkboxlist as selected or not, if they are to be selected, then add them to the session then as well. But do remember that the session is based on keys and value pairs so if you want to store each number you have to give them all a different key in the session. That or store the entire string and split them when you fetch the string back out of the session later.

//Populate 
DataSet dsCBL = Verif.GC(Convert.ToInt32(Session["ID"]));
for (int l = 0; l < dsCBL.Tables[0].Rows.Count; l++)
{
    ListItem listItem = new ListItem();
    listItem.Value = dsCBL.Tables[0].Rows[l][0].ToString();
    listItem.Text = dsCBL.Tables[0].Rows[l][1].ToString();
    listItem.Selected = false;

    if (listItem.Text == "Test1" || listItem.Text == "Test2") 
    { 
        listItem.Selected = true; 
        CheckBoxListTest.Items.Add(listItem); 

        // Ok, so it is selected and added, so add the value to the session as well
        Session.add("SomeKey" + l.ToString(), dsCBL.Tables[0].Rows[l][0]);    
    }
}



At the end of the loop you will have a checkboxlist with selected items and those selected items will have their values added to the session as well. Each value will have a key of "SomeKey" plus a number.

That is the general idea. :)
Was This Post Helpful? 0
  • +
  • -

#6 staindromeo   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 155
  • Joined: 21-October 10

Re: Select Certain ListItems in CheckboxList

Posted 01 August 2013 - 04:55 PM

The code right now returns a string so I am just looking for a way to split it out as I have it working but it only either looks at one value and ignores the rest or it takes all the values but doesn't split it. This is in reference to my last post.
Was This Post Helpful? 0
  • +
  • -

#7 staindromeo   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 155
  • Joined: 21-October 10

Re: Select Certain ListItems in CheckboxList

Posted 05 August 2013 - 08:26 AM

View PostMartyr2, on 01 August 2013 - 04:30 PM, said:

//Populate 
DataSet dsCBL = Verif.GC(Convert.ToInt32(Session["ID"]));
for (int l = 0; l < dsCBL.Tables[0].Rows.Count; l++)
{
    ListItem listItem = new ListItem();
    listItem.Value = dsCBL.Tables[0].Rows[l][0].ToString();
    listItem.Text = dsCBL.Tables[0].Rows[l][1].ToString();
    listItem.Selected = false;

    if (listItem.Text == "Test1" || listItem.Text == "Test2") 
    { 
        listItem.Selected = true; 
        CheckBoxListTest.Items.Add(listItem); 

        // Ok, so it is selected and added, so add the value to the session as well
        Session.add("SomeKey" + l.ToString(), dsCBL.Tables[0].Rows[l][0]);    
    }
}



At the end of the loop you will have a checkboxlist with selected items and those selected items will have their values added to the session as well. Each value will have a key of "SomeKey" plus a number.

That is the general idea. :)/>


Alright I tried this, and I still get only the first session variable value, instead of the multiple values in the session. Here's my code:

DataSet dsCBL = Verif.GC(Convert.ToInt32(Session["ID"]));

for (int l = 0; l < dsCBL.Tables[0].Rows.Count; l++)

{

    ListItem listItem = new ListItem();

    listItem.Value = dsCBL.Tables[0].Rows[l][0].ToString();

    listItem.Text = dsCBL.Tables[0].Rows[l][1].ToString();

    listItem.Selected = false;



    if (listItem.Text == "Test1" || listItem.Text == "Test2")

    {

        listItem.Selected = true;

        CheckBoxListTest.Items.Add(listItem);

 

        // SessionCBL is the session I am trying to add the values to

        Session.add("SessionCBL" + l.ToString(), dsCBL.Tables[0].Rows[l][0]);   

    }

}



Martyr's explanation seemed on point, but I don't know what I'm missing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1