Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkboxlsit problem 1

Status
Not open for further replies.

princesszea

Technical User
Aug 11, 2008
33
GB

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
 
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
faq732-7259
 
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
 
I have sorted the problem by adding
values = values.TrimEnd(',');
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top