[RESOLVED] Issue with displaying check box list values in a label or some other web control

I have trouble displaying the values of what options the user checked in a checkboxlist on a aspx page..... I am trying to do this programmatically through VB coding....This is what I have so far below in my code, but it just replaces the last value checked from the checkbox list.... I want it to display all the values checked onto one label....

 

If its not possible to display all the values of the checked values onto a label, what other web controls can I use to accomplish this...

Here is my code.....

 lblProLanguageInput.Text = " "

        If ProLanguages.Items(0).Selected Then

            lblProLanguageInput.Text = ProLanguages.Items(0).Text & "</br>"

        ElseIf ProLanguages.Items(1).Selected Then

            lblProLanguageInput.Text = ProLanguages.Items(1).Text & "</br>"

        ElseIf ProLanguages.Items(2).Selected Then

            lblProLanguageInput.Text = ProLanguages.Items(2).Text & "</br>"

        ElseIf ProLanguages.Items(3).Selected Then

            lblProLanguageInput.Text = ProLanguages.Items(3).Text & "</br>"

        End If

<div></div>

For every item that's checked, you're overwriting the value in the label.  instead you want to append to it.

So replace this: lblProLanguageInput.Text = ProLanguages.Items(1).Text & "</br>"

with this: lblProLanguageInput.Text = lblProLanguageInput.Text & ProLanguages.Items(1).Text & "</br>"

and do the same for item 2 & 3 as well.  Actually, it's better if you use something like a StringBuilder to build the string, and then at the end you set the .Text property of the label to the value in the stringbuilder.

I just tried to replace the code with what you recommended, but it didnt work....As far as stringbuilder, I am not familiar with using that before and I did some research, but I cannot get it to work.... Frown

Try using a multiline textbox to display the results instead as a test. Also, if you're familiar with debugging, put a breakpoint at the end of the method to check the .Text value and see if you're capturing everything you expect.

I figured it out using a For Each loop to display the checked items....

 

 

lblProLanguageInput.Text =

String

.Empty

 

For Each ListItem In

ProLanguages.Items

 

If (ListItem.selected)

Then

 

 

lblProLanguageInput.Text += ListItem.Text +

 

"<br />"

 

 

End

If

 

Next

Hello, this is a very simple solution to your question:

This is the code:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim i As Byte
        ListBox1.Items.Clear()
        For i = 0 To CheckBoxList1.Items.Count - 1
            If CheckBoxList1.Items(i).Selected Then ListBox1.Items.Add(CheckBoxList1.Items(i).Text)
        Next
    End Sub

And here the html:

 

<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem>Option1</asp:ListItem>
            <asp:ListItem>Option2</asp:ListItem>
            <asp:ListItem>Option3</asp:ListItem>
            <asp:ListItem>Option4</asp:ListItem>
        </asp:CheckBoxList>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <br />
        <br />
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
    
    </div>
    </form>
</body>

Hope it helps ;)

 

thoughts on "[RESOLVED] Issue with displaying check box list values in a label or some other web control"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>