Solved

CheckBoxList displaying multiple values from database

Posted on 2009-07-14
7
4,097 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 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 83

Accepted Solution

by:
CodeCruiser earned 300 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now