jeewai
asked on
CheckBoxList displaying multiple values from database
Good day everybody.
I had asked this question in the past but it was not formulated correctly and as a result I only got partial results.
I'm designing an app to store our staff Languages proficiency and came up with a formView with a checkboxlist to store multiple values. The cblSpokenLanguages has many items (English, French, Spanish) - see Ex 1. I figured I would store the values in 1 column only: Languages as comma separated values for ease of use after looping through each control selection.
So we are here: Coma Separated Values of languages spoken have been entered by users on table tblUsers, column Languages - they can select more than one choice and a simple method insert these choices on the column when they hit the insert form view button ie "Spanish, French" .
What I want now is to simply display them back on the cblSpokenLanguages CheckboxList to give the user an indication of what has been ticked (in formview read or edit) . I was given this code - see below . Where do I call it (page load ?) . Also the string was given "Dim preferences As String = "English, French, Spanish" but I need to refer to the Language column in the db. Is there another way to retrieve the info ?
As I'm new to ASP.net, I would appreciate if you could break down the steps and make it real easy to understand.
Thanks in advance for your kind and helpful attention
Best regards,
Jeewai
Ex1:
<asp:FormView ID="FormView1" runat="server" CssClass="formview" DefaultMode ="Insert" DataKeyNames="StaffID" DataSourceID="ObjectDataSo urce1">
<EditItemTemplate>
<asp:CheckBoxList ID="cblSpokenLanguagesEdit " runat="server" Text='<%# Bind("Type") %>'>
<asp:ListItem>French</asp: ListItem>
<asp:ListItem>English</asp :ListItem>
<asp:ListItem>Spanish</asp :ListItem> </asp:CheckBoxList> ...
</EditItemTemplate>
<InsertItemTemplate>
<asp:CheckBoxList ID="cblSpokenLanguages" runat="server" Text='<%# Bind("Type") %>'>
<asp:ListItem>French</asp: ListItem>
<asp:ListItem>English</asp :ListItem>
<asp:ListItem>Spanish</asp :ListItem>
</asp:CheckBoxList> ...
</InsertItemTemplate>
<ItemTemplate>
<asp:CheckBoxList ID="cblSpokenLanguagesRead " runat="server" Text='<%# Bind("Type") %>'>
<asp:ListItem>French</asp: ListItem>
<asp:ListItem>English</asp :ListItem>
<asp:ListItem>Spanish</asp :ListItem> ...
</asp:CheckBoxList> </ItemTemplate>
</asp:FormView>
I had asked this question in the past but it was not formulated correctly and as a result I only got partial results.
I'm designing an app to store our staff Languages proficiency and came up with a formView with a checkboxlist to store multiple values. The cblSpokenLanguages has many items (English, French, Spanish) - see Ex 1. I figured I would store the values in 1 column only: Languages as comma separated values for ease of use after looping through each control selection.
So we are here: Coma Separated Values of languages spoken have been entered by users on table tblUsers, column Languages - they can select more than one choice and a simple method insert these choices on the column when they hit the insert form view button ie "Spanish, French" .
What I want now is to simply display them back on the cblSpokenLanguages CheckboxList to give the user an indication of what has been ticked (in formview read or edit) . I was given this code - see below . Where do I call it (page load ?) . Also the string was given "Dim preferences As String = "English, French, Spanish" but I need to refer to the Language column in the db. Is there another way to retrieve the info ?
As I'm new to ASP.net, I would appreciate if you could break down the steps and make it real easy to understand.
Thanks in advance for your kind and helpful attention
Best regards,
Jeewai
Ex1:
<asp:FormView ID="FormView1" runat="server" CssClass="formview" DefaultMode ="Insert" DataKeyNames="StaffID" DataSourceID="ObjectDataSo
<EditItemTemplate>
<asp:CheckBoxList ID="cblSpokenLanguagesEdit
<asp:ListItem>French</asp:
<asp:ListItem>English</asp
<asp:ListItem>Spanish</asp
</EditItemTemplate>
<InsertItemTemplate>
<asp:CheckBoxList ID="cblSpokenLanguages" runat="server" Text='<%# Bind("Type") %>'>
<asp:ListItem>French</asp:
<asp:ListItem>English</asp
<asp:ListItem>Spanish</asp
</asp:CheckBoxList> ...
</InsertItemTemplate>
<ItemTemplate>
<asp:CheckBoxList ID="cblSpokenLanguagesRead
<asp:ListItem>French</asp:
<asp:ListItem>English</asp
<asp:ListItem>Spanish</asp
</asp:CheckBoxList> </ItemTemplate>
</asp:FormView>
Public Sub FindPeference()
Dim preferences As String = "English, French, Spanish"
Dim li As ListItem
Dim preference() As String = preferences.Split(",")
'Clear any previous setting
CheckBoxList1.SelectedIndex = -1
For Each pref As String In preference
li = CheckBoxList1.Items.FindByValue(pref)
If li Is Nothing = False Then li.Selected = True
Next
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Shahprabal: and CodeCruiser: Thanks a lot for the great heads up !
I was about to attribute points to you 2 but felt that I understand 75% and would need some additional explanations... Again, I'm new to classes and property stuffs. Please explain like you would to a dummy :-)
So in resume please
1. can you provide the code to Create a custom control that inherits checkboxlist control. It can be real simploe but I just need to see that. How do I inherit.. again some lines of code will do the job, best if you could customize it to my example ie cblSpokenLanguages. Also, you notice I named cblSpokenLanguages, cblSpokenLanguagesEdit and cblSpokenLanguagesRead to tell apart depending if in read or edit mode.
2. I got the language property from CodeCruiser. Well appreciated... now Where do I call it (code behind) . New class file ?
3. How and where do I bind the SelectedLanguages Property to the database (Bind("Type") ? open dataset ? If you need add info, II'll gladly chip in. Some lines would do wonders.
Thanks in advance for your follow up assistance. I sincerely appreciate the few minutes you set aside to help out. Hope this time I will get it picture perfect :-)
Best regards,
Jeewai
I was about to attribute points to you 2 but felt that I understand 75% and would need some additional explanations... Again, I'm new to classes and property stuffs. Please explain like you would to a dummy :-)
So in resume please
1. can you provide the code to Create a custom control that inherits checkboxlist control. It can be real simploe but I just need to see that. How do I inherit.. again some lines of code will do the job, best if you could customize it to my example ie cblSpokenLanguages. Also, you notice I named cblSpokenLanguages, cblSpokenLanguagesEdit and cblSpokenLanguagesRead to tell apart depending if in read or edit mode.
2. I got the language property from CodeCruiser. Well appreciated... now Where do I call it (code behind) . New class file ?
3. How and where do I bind the SelectedLanguages Property to the database (Bind("Type") ? open dataset ? If you need add info, II'll gladly chip in. Some lines would do wonders.
Thanks in advance for your follow up assistance. I sincerely appreciate the few minutes you set aside to help out. Hope this time I will get it picture perfect :-)
Best regards,
Jeewai
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi CodeCuiser,
Thanks for the lines for the class declaration but I'm still lost. My code - I've followed your advices (see below) shows many errors. Should I name the class Spokenlanguages? Were do I call it. I don't know the steps to follow and fail to grasp the essence of it.
Would you be able to create a few files and share them. (default.aspx with my simple formview, class file, etc...) . I sincerely do not want to give you a lot of work but I believe that if you make a simple workable example on how to retrieve these damn! :-) values to the chekboxlist I will get it spot on !
Thanks immensely,
Jeewai
Thanks for the lines for the class declaration but I'm still lost. My code - I've followed your advices (see below) shows many errors. Should I name the class Spokenlanguages? Were do I call it. I don't know the steps to follow and fail to grasp the essence of it.
Would you be able to create a few files and share them. (default.aspx with my simple formview, class file, etc...) . I sincerely do not want to give you a lot of work but I believe that if you make a simple workable example on how to retrieve these damn! :-) values to the chekboxlist I will get it spot on !
Thanks immensely,
Jeewai
Imports Microsoft.VisualBasic
Public Class cblSpokenLanguages Inherits CheckListBox
'the property code goes here
Public Property SelectedLanguages() As String
Get
Dim Ls As String
For Each s In Items
If s.Checked Then
Ls = Ls & s.Value & ","
End If
Next
End Get
Set(ByVal Value As String)
If Value.Length > 0 Then
If Value.Split(",").Count > 0 Then
For Each L As String In Value.Split(",")
For Each i In Items
If i.Value = L Then
i.Checked = True
End If
Next
Next
Else
For Each i InItems
If i.Value = Value Then
i.Checked = True
End If
Next
End If
End If
End Set
End Property
Public Sub New()
MyBase.new()
End Sub
End Class
I think the inherits should be CheckEDListBox (you missed ED). Following link would give you an idea of how to inherit existing controls
http://www.codeproject.com/KB/grid/custCheckedListBox.aspx
http://www.codeproject.com/KB/grid/custCheckedListBox.aspx
ASKER
Thanks for the link. That give me some hints. In the meantime, I will investigate other simple ways to resolve this issue and will post back my solutions on this posts.
Open in new window