?
Solved

ASP.NET CheckboxLists and Arrays (C#)

Posted on 2004-11-02
11
Medium Priority
?
881 Views
Last Modified: 2012-05-05
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.

0
Comment
Question by:NaderYacoub
  • 6
  • 5
11 Comments
 
LVL 4

Expert Comment

by:jcrumble
ID: 12478005
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
 
LVL 2

Author Comment

by:NaderYacoub
ID: 12478118
I guess my big question is how do you go from an Array to a comma delimited list?
0
 
LVL 4

Expert Comment

by:jcrumble
ID: 12478312
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Author Comment

by:NaderYacoub
ID: 12486363

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
 
LVL 4

Expert Comment

by:jcrumble
ID: 12486567
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
 
LVL 4

Expert Comment

by:jcrumble
ID: 12486612
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
 
LVL 2

Author Comment

by:NaderYacoub
ID: 12486674
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
 
LVL 2

Author Comment

by:NaderYacoub
ID: 12486685
Also... I visited that article earlier today
0
 
LVL 4

Expert Comment

by:jcrumble
ID: 12486784
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
 
LVL 4

Accepted Solution

by:
jcrumble earned 980 total points
ID: 12486797
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
 
LVL 2

Author Comment

by:NaderYacoub
ID: 12487532
I don't think I am gonna find the answer I am looking for, but you're solution does answer the initial question.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question