CheckBoxList -- Iterating Through Items

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default CheckBoxList -- Iterating Through Items

    Can someone tell me how I can iterate through all of the Checkboxes included
    within a checkbox list?


    The code I currently have is as follows (it doesnt work for some reason, it
    returns that no items are selected):

    foreach(ListItem objItem in chkCategory.Items)
    {
    if(objItem.Selected == true)
    {
    category += objItem.Text;
    }
    }


    Thanks,

    Ron


    Ron Guest

  2. Similar Questions and Discussions

    1. Creating master page where global items arearranged ABOVE local items
      When you create a mater page, obviously all the items are arranged below anything on the individual pages. Is there a way of creating a master...
    2. Iterating through an XML Dataset
      Hi.. I am new to trying to integrate any kind of data with Flash. I have Flash 8 Professional. I am fluent in ASP and JavaScript so I am...
    3. Iterating custom controls
      Here's a simple problem that's eating my lunch: I need to write a base class method in ASP.NET that will iterate the page and find all controls...
    4. iterating through hash of hashes
      I'd like to iterate through a hash of hashes and get all the values. How do I do that? The following works for me. But it seems a bit convoluted...
    5. Iterating through controls (VB)
      I have been trying to iterate through the controls on my form and preset all textboxes to a cssClass. Unfortunately, I have been unable to figure...
  3. #2

    Default Re: CheckBoxList -- Iterating Through Items

    Quote Originally Posted by Ron View Post
    Can someone tell me how I can iterate through all of the Checkboxes included
    within a checkbox list?


    The code I currently have is as follows (it doesnt work for some reason, it
    returns that no items are selected):

    foreach(ListItem objItem in chkCategory.Items)
    {
    if(objItem.Selected == true)
    {
    category += objItem.Text;
    }
    }


    Thanks,

    Ron
    There's nothing obvious that you have done wrong, it could be that you are setting the checkbox items to unchecked in the page load, but not wrapping them in a if (!IsPostBack) { } conditional, so the checkboxes are regenerated each time you load the page. Another possiblity is that if you are using databinding then the code above is firing before the databinding, in which can you would need to put this code in a ondatabound() method.

    without the full source code it is impossible to tell what is the cause.
    MYKEBLACK Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139