Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CheckBoxList displaying multiple values from database

Posted on 2009-07-14
7
Medium Priority
?
4,415 Views
Last Modified: 2013-11-26
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="ObjectDataSource1">
<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>


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

Open in new window

0
Comment
Question by:jeewai
  • 3
  • 3
7 Comments
 
LVL 14

Assisted Solution

by:shahprabal
shahprabal earned 600 total points
ID: 24849462
You can create a custom checkboxlist control where you create a languageSelected property. This property will accept string value, and this is where you do a split(,) to read different values in the comma seperated string. If any of the value matches the name of the checkbox then set the checked property to true.
This way you just have to bind the languageSelected property of custom checkboxlist to the db column value and you are all set.
You can also overload the checked property and add one that accepts string and follow the steps mentioned above.
So here are the steps again:
1) Create a custom control that inherits checkboxlist control.
2) Create a languageSelected property.
3) In this property parse the language string and check if it matches the name, if it does set checked = true.
4) Bind the languageSeletected property to the dataset column.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24850323
Further to the Expert suggestion above, just inherit the checkedlistbox and use a property. It would make it easier because you are using binding and not populate from the server side. The property could be impremented as follows



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(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

Open in new window

0
 

Author Comment

by:jeewai
ID: 24853804
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Accepted Solution

by:
CodeCruiser earned 900 total points
ID: 24857560
To inherit from the CheckedListBox, you would create a class file like this

Public Class cblSpokenLanguages
          Inherits CheckedListBox

'the property code goes here

public sub new()
mybase.new()
end sub
...
End Class

I am not sure whether you need to do anything extra to bind this property to the database.
0
 

Author Comment

by:jeewai
ID: 24858237
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
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

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24858387
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
0
 

Author Closing Comment

by:jeewai
ID: 31603217
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question