Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

CheckBoxList - SET Selected

  Asked By: Hondo    Date: Dec 22    Category: Asp.net    Views: 6464
  

I have a checkboxlist on a form that is populated by a databind. The
users need to be able to change previous checks they made to the
checkboxlist. First they enter in their info, and the the db goes to
look up what items in the checklist they have selected before. I
know how to GET the selected items, but I can't seem to SET the
items to have a checkmark in them if certain criteria is met (i.e.
the listitem value equals the value we're looping through in the
database via a datareader. I tried setting the listitem.selected =
true, but the checkboxes still aren't checked in the checkboxlist,
even though the value from the checkboxlist index equals the db
value. Here's the code I tried:


(for each listItem in the checkboxlist)
For Each li In Me.chkEvents.Items

(if the listitem value (i.e. 1, 2, etc) equals the
datareader(field index) value (i.e. 1, 2, etc) then
set it to Selected)


If li.Value = tmpDRValue Then
chkEvents.Items(li.Value).Selected = True
End If

Next

I know they are matching, because this If then evaluates to true
when the values do match. It just doesn't put a checkmark into the
checkbox in the list. Can anyone help

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Julia Hughes     Answered On: Dec 22

try this.

this.COMPANY.Items.FindByValue(StrAirline).Selected = true;

you can also find the item by text.

hope it helps.

 
Answer #2    Answered By: Scarlett Hughes     Answered On: Dec 22

I figured out what happened. I was
getting an error when I went to close the cmd object connection, but
until I was viewing in the local browser in VS.NET, as opposed to
building and browsing, I didn't see it. That was why the checks
weren't showing up in the checkboxes. One way to set  the Checkbox
list is like this:

'iterate through the check boxes in the checkbox list  - "li" has
been declared as a listitem. chkEvents is the name of the
checkboxlist


For Each li In Me.chkEvents.Items
'if the list item value equals the db value we're
comparing(tmpDRValue is a variable set to DR1.GetValue(7), the 8th
element in the Datareader)

If li.Value = tmpDRValue Then
'set the Selected to true and this checkbox will
be checked.

li.Selected = True


End If

'iterate to the next checkbox in the list
Next

End While

 
Didn't find what you were looking for? Find more on CheckBoxList - SET Selected Or get search suggestion and latest updates.




Tagged: