ASP.NET CheckboxLists and Arrays (C#)

Hello all,

I need to know what the best way is to convert the "selected items" of a CheckboxList to an Array. I also need to know how to convert that array to a string that is a comma-delimited list of that Array.

I am trying to avoid loops.

LVL 2
NaderYacoubAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jcrumbleCommented:
Well,  unfortunately I think you are going to have to use a loop or at least a foreach statement.  What do you think about creating the comma delimited list first then going to the array?  You could use something like:

for(int i=0; i<ResourceDisp.Items.Count; i++){
      if(ResourceDisp.Items[i].Selected){ ViewState["ResDisp1"] += ResourceDisp.Items[i].Text + ","; }
}



Where ResourceDisp is your CheckboxList and ViewState["ResDisp1"] is your comma delimited string.  You might also want to do some counting and place an if statement on the inside of your loop to control whether there is a comma attached to the end of your comma delimited string or not as well.

You could then do a split: on the comma to build your array.

Check out: http://www.informit.com/articles/article.asp?p=31938&seqNum=14 for an example of using the split function to build an array.

Good Luck,
Jeff
0
NaderYacoubAuthor Commented:
I guess my big question is how do you go from an Array to a comma delimited list?
0
jcrumbleCommented:
Well, here's a function I use to go from a ListBox to an array and sort it:

            void SortList(ListBox control) {
                  ViewState["Retainer"] = "";
                  SortedList ThisList = new SortedList();
                  ListItem[] myListItemArray = new ListItem[control.Items.Count];
                  control.Items.CopyTo(myListItemArray, 0);
                  foreach(ListItem i in myListItemArray){
                    ThisList.Add(i.Text,i.Value);
                    ViewState["Retainer"] += i.Value+";";
                  }
                  control.Items.Clear();
                  control.DataSource=ThisList;
                  control.DataValueField="Value";
                  control.DataTextField="Key";
                  control.DataBind();
            }

Since I've got to go vote....like now, with a slight variation on the code I posted above you should be able to merge the two examples.  Let me know if you need me to put it together for you.

Good Luck,
Jeff
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

NaderYacoubAuthor Commented:

my thing is this...

how do I go from "array" to type of "string[]"
do I have to loop... is there any other way?

can you provide code please?
0
jcrumbleCommented:
The type string[] is an array.  I'm not really sure what your asking.

Here's an article that might help you a bit:
http://www.c-sharpcorner.com/Code/2002/July/WorkingWithArrays.asp
0
jcrumbleCommented:
If you can be more specific about exactly what it is your trying to do.  I understand that your source is a CheckBoxList and that you want to convert a CheckboxList to an Array, but you also mentioned going to a comma-delimited string.  What exactly do you want to do with this array/comma-delimited string, and does the order you to your conversion in matter( assuming you really need to retain both)?
0
NaderYacoubAuthor Commented:
on Array you cannot use String.Join but you can on string[]

I want to ultimately get a string (not string[]) that is a comma-delimited list representation of the array
0
NaderYacoubAuthor Commented:
Also... I visited that article earlier today
0
jcrumbleCommented:
I know this contains loops but how about something like:

int j=0, m=0;
String[] eventnmes = new String[j];
// where ResourceDisp is your CheckBoxList

for(int i=0; i<ResourceDisp.Items.Count; i++){
  if(ResourceDisp.Items[i].Selected){
    j++;
  }
}

for(int k=0; k<ResourceDisp.Items.Count; k++){
  if(ResourceDisp.Items[k].Selected){
     eventnmes.SetValue(ResourceDisp.Items[k].Text, m);
     m++;
  }
}
0
jcrumbleCommented:
oops.....I put my string[] in the wrong location when I was cleaning up my thought for you.  Let's try that again.....

int j=0, m=0;
// where ResourceDisp is your CheckBoxList
for(int i=0; i<ResourceDisp.Items.Count; i++){
  if(ResourceDisp.Items[i].Selected){
    j++;
  }
}
String[] eventnmes = new String[j];
for(int k=0; k<ResourceDisp.Items.Count; k++){
  if(ResourceDisp.Items[k].Selected){
     eventnmes.SetValue(ResourceDisp.Items[k].Text, m);
     m++;
  }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NaderYacoubAuthor Commented:
I don't think I am gonna find the answer I am looking for, but you're solution does answer the initial question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.