Connecting Tech Pros Worldwide Forums | Help | Site Map

Checkbox list

Member
 
Join Date: Jun 2007
Location: Hyderabad
Posts: 73
#1: Oct 6 '08
Hi all,
Good Day

am working on asp.net 2.0, c#

i stored checkbox list selected values separating with . as
Expand|Select|Wrap|Line Numbers
  1. string s = null;
  2.          for (int i = 0; i < chlistDomains.Items.Count; i++)
  3.          {
  4.              if (chlistDomains.Items[i].Selected)
  5.              {
  6.                  s = s + chlistDomains.Items[i].Value + ".";
  7.              }
  8.          }
  9.  
i retrieved that value in to a string variable

now am trying to selectcheckbox list values which are in that string variable
i wrote following code but it is selecting last value only
Expand|Select|Wrap|Line Numbers
  1. string[] sp1 = pref.Split('.');
  2.             for (int j = 0; j < sp1.Length; j++)
  3.             {
  4.                 clistPrefferedEmployement.SelectedValue = Convert.ToString(sp1[j]);
  5.             }
please suggest me

Thanks in advance

Newbie
 
Join Date: Oct 2008
Posts: 12
#2: Oct 6 '08

re: Checkbox list


The .SelectedValue method will only select a single value..

you should select each item specifically:

Also, you could use an array to capture your values:

Expand|Select|Wrap|Line Numbers
  1. string[] s = new string(chlistDomains.Items.Count());
  2.  
  3.         for (int i = 0; i < chlistDomains.Items.Count; i++)
  4.         {
  5.             if (chlistDomains.Items[i].Selected)
  6.             {
  7.                 s[i] = chlistDomains.Items[i].Value;
  8.             }
  9.         }
  10.  
  11.         foreach (string item in s)
  12.         {
  13.             clistPrefferedEmployement.Items[theitemhere].Value = item;
  14.         }
  15.  
Hope this helps?
Member
 
Join Date: Jun 2007
Location: Hyderabad
Posts: 73
#3: Oct 6 '08

re: Checkbox list


Expand|Select|Wrap|Line Numbers
  1. foreach (string item in s)
  2. {
  3. clistPrefferedEmployement.Items[theitemhere].Value = item;
  4. }
  5.  
in this stmt wt was theitemhere?

Quote:

Originally Posted by snester

The .SelectedValue method will only select a single value..

you should select each item specifically:

Also, you could use an array to capture your values:

string[] s = new string(chlistDomains.Items.Count());

for (int i = 0; i < chlistDomains.Items.Count; i++)
{
if (chlistDomains.Items[i].Selected)
{
s[i] = chlistDomains.Items[i].Value;
}
}

foreach (string item in s)
{
clistPrefferedEmployement.Items[theitemhere].Value = item;
}

Hope this helps?

Newbie
 
Join Date: Oct 2008
Posts: 12
#4: Oct 6 '08

re: Checkbox list


I'm sorry, my code was slightly incorrect before, hopefully this should be self explainitory..

Expand|Select|Wrap|Line Numbers
  1. int count = 0;
  2.             int[] n = new int[chlistDomains.Items.Count];
  3.             int trackPos = 0;
  4.             string[] s = new string[chlistDomains.Items.Count];
  5.  
  6.             for (int i = 0; i < chlistDomains.Items.Count; i++)
  7.             {
  8.                 if (chlistDomains.Items[i].Selected)
  9.                 {
  10.                     count++;
  11.                     n[i] = trackPos;
  12.                     s[i] = chlistDomains.Items[i].Value;
  13.                 }
  14.                 trackPos++;
  15.             }
  16.  
  17.  
  18.             int listPos = 0;
  19.             foreach (string item in s)
  20.             {
  21.                 if (clistPrefferedEmployement.Items[n[listPos]].Value == item)
  22.                 {
  23.                     clistPrefferedEmployement.Items[n[listPos]].Selected = true;
  24.                 }
  25.                 listPos++;
  26.             }
I have made assumptions based on your original post, I hope they are correct:
- the items in chlistDomains and clistPrefferedEmployement are the same and in the same order.
- the items are already in the clistPrefferedEmployement list prior to the event.

(if not, you could always add the items into clistPrefferedEmployement) just replace the foreach statement with:

Expand|Select|Wrap|Line Numbers
  1.             int listPos = 0;
  2.             foreach (string item in s)
  3.             {
  4.                 clistPrefferedEmployement.Items.Add(item);
  5.             }
Hope this is clearer than my first answer?
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#5: Oct 6 '08

re: Checkbox list


Both of you need to start using code tags when you post. snester, you're new to the site, so there is some excuse, but Vajrala Narendra, you have over 50 posts. You should know this by now. I see you were trying to use [b] tags. This is for bold text. [code] is what you need to use for code.

Code tags are not optional, they are required. Please start using them.

MODERATOR
Reply