Answered by:
Showing values in check box List from single column in database-VB.NET

Question
-
User-1578974752 posted
If my check box list values are saved to database as 1 column(with commas) ,then upon clicking Open button, how can I show these saved values in a checkbox list again
Appreciate the Help
Thursday, November 8, 2018 5:26 AM
Answers
-
User-1509636757 posted
You should fetch the column value from database, split it by comma and search for each splitted value in CheckBoxList Items and make it Selected property to true.
Below is a somewhat similar kind of code snippet that demonstrate to show selected list values in a CheckBoxLiist control:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:CheckBoxList runat="server" ID="CheckBoxList1"> <asp:ListItem Text="text1" Value="1" /> <asp:ListItem Text="text2" Value="2" /> <asp:ListItem Text="text3" Value="3" /> <asp:ListItem Text="text4" Value="4" /> <asp:ListItem Text="text5" Value="5" /> </asp:CheckBoxList> </div> </form> </body> </html>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not IsPostBack Then BindRepeaterData() End If End Sub Private Sub BindRepeaterData() Dim stringFromDatabase As String = "4,5" For Each item As String In stringFromDatabase.Split(","c) Dim lstItem As ListItem = CheckBoxList1.Items.FindByValue(item) If lstItem IsNot Nothing Then lstItem.Selected = True Next End Sub
hope that helps./.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, November 8, 2018 12:13 PM
All replies
-
User-1509636757 posted
You should fetch the column value from database, split it by comma and search for each splitted value in CheckBoxList Items and make it Selected property to true.
Below is a somewhat similar kind of code snippet that demonstrate to show selected list values in a CheckBoxLiist control:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:CheckBoxList runat="server" ID="CheckBoxList1"> <asp:ListItem Text="text1" Value="1" /> <asp:ListItem Text="text2" Value="2" /> <asp:ListItem Text="text3" Value="3" /> <asp:ListItem Text="text4" Value="4" /> <asp:ListItem Text="text5" Value="5" /> </asp:CheckBoxList> </div> </form> </body> </html>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not IsPostBack Then BindRepeaterData() End If End Sub Private Sub BindRepeaterData() Dim stringFromDatabase As String = "4,5" For Each item As String In stringFromDatabase.Split(","c) Dim lstItem As ListItem = CheckBoxList1.Items.FindByValue(item) If lstItem IsNot Nothing Then lstItem.Selected = True Next End Sub
hope that helps./.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, November 8, 2018 12:13 PM -
User61956409 posted
Hi shsu,
If my check box list values are saved to database as 1 column(with commas) ,then upon clicking Open button, how can I show these saved values in a checkbox list againYou could refer to the following code snippet to dynamically set items of CheckBoxList to selected.
'in your code, the selected items should be retrieved from your database Dim selecteditems As String = "type2,type3" For Each item As ListItem In CheckBoxList1.Items If selecteditems.Split(",").Contains(item.Text) Then item.Selected = True End If Next
Test Result:
With Regards,
Fei Han
Friday, November 9, 2018 1:47 AM