glebn
asked on
Checkboxlist - set checked status from database
I have a checkboxlist bound to an object data source in a VB ASP.NET 2.0 environment.
My object datasource returns a three column sqlDataReader with the following columns: "TagName", "TagID", "Selected". The "Selected" column is boolean. I set the "DataTextField" = "TagName" and the "DataValueField" = "TagID". Unfortunately there is no "DataCheckedField" property to set the selected status of the check box to the value from the "Selected" column.
When I load the checkboxlist I want the items for which the "Selected" field from the database equals "True" to be selected in the checkboxlist. What is the easiest way to do this? If the answer involves setting it with code which it probably does, please use VB.
Note: I have no problem writing back the user's selections to the database.
My object datasource returns a three column sqlDataReader with the following columns: "TagName", "TagID", "Selected". The "Selected" column is boolean. I set the "DataTextField" = "TagName" and the "DataValueField" = "TagID". Unfortunately there is no "DataCheckedField" property to set the selected status of the check box to the value from the "Selected" column.
When I load the checkboxlist I want the items for which the "Selected" field from the database equals "True" to be selected in the checkboxlist. What is the easiest way to do this? If the answer involves setting it with code which it probably does, please use VB.
Note: I have no problem writing back the user's selections to the database.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for responding Renju, but I'm sorry your comments didn't answer my question. Your second comment was too vague and it didn't use VB. Telling me to loop through and set the box tells me nothing about how to do this most effectively. Again, thanks for responding.
I solved my own problem. My fundamental mistake was trying to use one datasource to build the list and set the values. Being an ASP.NET newbie I did this because I expected to be able to use the boolean "Selected" field to set the check box "checked" status which I now see that I can't. Instead, I used a datasource to populate the CheckboxList which included all possible rows ignoring the "Selected" field. I then created a different data source pulling only the rows for which selected was true. This second data source I used for the sqlReader in the code below to loop through the checkboxlist items during page load to check the appropriate items.
If sqlReader.HasRows Then
Do While sqlReader.Read()
For Each li As ListItem In Me.chkblCategories.Items
If li.Value = sqlReader("TagID") Then
li.Selected = True
End If
Next
Loop
Else
' If sqlReader doesn't have rows then no items should be selected.
For Each li As ListItem In Me.chkblCategories.Items
li.Selected = False
Next
End If
sqlreader = Nothing
If anybody has suggestions for improving this or alternate approaches to the problem I would be glad to award the points.
I solved my own problem. My fundamental mistake was trying to use one datasource to build the list and set the values. Being an ASP.NET newbie I did this because I expected to be able to use the boolean "Selected" field to set the check box "checked" status which I now see that I can't. Instead, I used a datasource to populate the CheckboxList which included all possible rows ignoring the "Selected" field. I then created a different data source pulling only the rows for which selected was true. This second data source I used for the sqlReader in the code below to loop through the checkboxlist items during page load to check the appropriate items.
If sqlReader.HasRows Then
Do While sqlReader.Read()
For Each li As ListItem In Me.chkblCategories.Items
If li.Value = sqlReader("TagID") Then
li.Selected = True
End If
Next
Loop
Else
' If sqlReader doesn't have rows then no items should be selected.
For Each li As ListItem In Me.chkblCategories.Items
li.Selected = False
Next
End If
sqlreader = Nothing
If anybody has suggestions for improving this or alternate approaches to the problem I would be glad to award the points.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Both work fine. I prefer the For-Next loop because it is easier to read--don't know which method would be faster but the number of checkboxes are so small that it doesn't matter.
P.S. My Else clause was written a little to late at night ;) -- as written it doesn't clear checkboxes properly in some cases. I fixed it by removing the Else clause entirely and putting the clear all checkboxes code before the initial If-Then.
Thanks for the comment.
P.S. My Else clause was written a little to late at night ;) -- as written it doesn't clear checkboxes properly in some cases. I fixed it by removing the Else clause entirely and putting the clear all checkboxes code before the initial If-Then.
Thanks for the comment.
ASKER
I actually decided to use my solution posted above, but appreciate both responses which were correct solutions. I gave sandip132 most of the points because s/he gave a detailed solution with VB code.
for (int i = 0; i < CheckBoxList1.Items.Count;
{
CheckBoxList1.Items[i].Sel
}
regards
Renju