468,170 Members | 2,185 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,170 developers. It's quick & easy.

Checkbox list

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
Oct 6 '08 #1
4 901
snester
12
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?
Oct 6 '08 #2
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?

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?
Oct 6 '08 #3
snester
12
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?
Oct 6 '08 #4
Curtis Rutland
3,256 Expert 2GB
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
Oct 6 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Steven | last post: by
6 posts views Thread by Daz | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.