Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...The level of expertise is awesome. The nature in which people respond is professional helpful and not the least condescending. I can't say that for most forums..."

Geography

Where in the world do Tek-Tips members come from?
princesszea (TechnicalUser)
2 Sep 10 14:13

 Hi,

I'm using the code below to get the values of all the checkboxlist on my page .

CODE

                else if (cntrl.GetType() == typeof(CheckBoxList))
                {
                    if (!string.IsNullOrEmpty(((CheckBoxList)cntrl).Text))
                    {
                        ids = ((CheckBoxList)cntrl).ID;
                        values = ((CheckBoxList)cntrl).Items.ToString();
                    }
                }

The problem I'm having is if I had a checkbox list called chkage
Using the above method how do I add the selected values to
the code below

CODE

  if (!string.IsNullOrEmpty(ids))
                {
                    Session[id] = value;

                    if (Mapp.ContainsKey(ids))
                    {
                        //now get the db parameter name
                        string newValue = Mapp[ids];

                        if (!criteria.ContainsKey(newValue))
                            criteria.Add(newValue, value);

                    }
                }

Thanks in advance
Helpful Member!  jmeckley (Programmer)
2 Sep 10 14:48
I'm assuming this is a web control since you are storing values in Session. if this is correct, your 1st problem is

CODE

values = ((CheckBoxList)cntrl).Items.ToString();
the value will be System.Web.WebControls.UI.ListItemCollection not the selected values.

since you are looping through controls in a generic fashion you will need to store the control id and the selected values in a dictionary and put the dictionary in session.

when you pull the dictionary out of the session you can then find the control based on the key in the dictionary and set the values based on the value in the dictionary.

however I would question the overall approach, why are you looping through controls, rather than referencing them explicitly? it make the code more difficult to read and therefor less maintainable.

if you want to store series of values in session related to a specific page, create an object which reflects those values, store the object in session and reference the properties. something like this

CODE

[Serializable]
class MyPageDto
{
   public MyPageDto()
   {
      CheckBoxListA = new List<string>();
      CheckBoxListB = new List<string>();
   }

   public List<string> CheckBoxListAValues {get; private set;}
   public List<string> CheckBoxListBValues {get; private set;}
   ....
}
then you can use it like this

CODE

var dto = new MyPageDto();

PutSelectedValuesIn(dto.CheckBoxListAValues, CheckBoxListA);
PutSelectedValuesIn(dto.CheckBoxListBValues, CheckBoxListB);

Session.Add("my page dto", dto);

private static void PutSelectedValuesIn(ICollection<string> collection, ListBaseControl control)
{
   foreach(var item in control.Items)
   {
      if(item.Checked == false) continue;
      collection.Add(item.Value);
   }
}
then you can pull the values out

CODE

var dto = (MyPageDto)Session["my page dto"];
SetSelectedValue(dto.CheckBoxListAValues, CheckBoxListA);
SetSelectedValue(dto.CheckBoxListBValues, CheckBoxListB);

private static void SetSelectedValue(IEnumerable<string> collection, ListBaseControl control)
{
   foreach(var item in collection)
   foreach(var item in control.Items)
   {
      if(item.Value != item) continue;
      item.Selected = true;
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive

princesszea (TechnicalUser)
3 Sep 10 5:35
Thanks Jason,

 
princesszea (TechnicalUser)
3 Sep 10 7:47
I used the code below but i get 2,3, but i need 2,3

[code]
   else if (cntrl.GetType() == typeof(CheckBoxList))
                {
                    if (!string.IsNullOrEmpty(((CheckBoxList)cntrl).Text))
                    {
                        id = ((CheckBoxList)cntrl).ID;
                        
                    foreach (ListItem item in ((CheckBoxList)cntrl).Items)
                    {
                        if (item.Selected)
                        {
                            value += item.Value + ",";  
                     
                    
                        }
                    }
                   
                    
                  }
                }
[\code]

Thanks
princesszea (TechnicalUser)
3 Sep 10 8:02
I have sorted the problem by adding
values = values.TrimEnd(',');

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close