Solved

ASP.net VB.net CheckBoxList checked items dissapear on button click

Posted on 2016-10-20
8
35 Views
Last Modified: 2016-10-21
Hi

I have a CheckBoxList that I have added to my web form using thefollowing code:

  Dim oCheckBoxList As New CheckBoxList
  Me.Panel_Controls.Controls.Add(oCheckBoxList)
             

When the user clicks a button I try to gather the selected values into a string but for some reason none of the checked items are gathered.
After the postback caused by the button click all of the items in the CheckListBox are unchecked. Is there a way to prevent this

                        Dim oCheckBoxList As CheckBoxList = CType(Me.Panel_Controls.FindControl(oControlName), CheckBoxList)
                        sValue = ""
                        For Each li As ListItem In oCheckBoxList.Items
                            If li.Selected Then
                                If sValue = "" Then
                                    sValue = li.Text
                                Else
                                    sValue = sValue & "," & li.Text
                                End If
                            Else
                                'do whatever you need
                            End If
                        Next
0
Comment
Question by:murbro
8 Comments
 
LVL 33

Expert Comment

by:paulmacd
ID: 41852080
Do you have the ViewStateMode="Enabled" set in the @Page declaration?  Also, make sure the control is set to runat="server".
0
 
LVL 35

Expert Comment

by:YZlat
ID: 41852113
Where do you add your CheckBoxList? Page_Init? Page_Load? Also could you post your code for Page_Load and Button_Click events?
0
 

Author Comment

by:murbro
ID: 41852557
I use the following code in Page_Load

Dim oCheckBoxList As New CheckBoxList
  Me.Panel_Controls.Controls.Add(oCheckBoxList)

and the following in the button click

                Dim oCheckBoxList As CheckBoxList = CType(Me.Panel_Controls.FindControl(oControlName), CheckBoxList)
                        sValue = ""
                        For Each li As ListItem In oCheckBoxList.Items
                            If li.Selected Then
                                If sValue = "" Then
                                    sValue = li.Text
                                Else
                                    sValue = sValue & "," & li.Text
                                End If
                            Else
                                'do whatever you need
                            End If
                        Next
0
 

Author Comment

by:murbro
ID: 41852569
Also. Thanks paulmacd. I did what you said but unfortunately that didn't work
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 41852871
Hi murbro,

1. Please check the code  Page_Load is called more than once.
2. Bind Checkboxlist inside IsPostback of Page_Load.

If Not Page.IsPostBack Then

        ' Bind the checkbox list here
    End If

Open in new window

0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 41853700
@murbro, as I suspected you are not checking for IsPostBack in your script. If you do not use IsPostBack,
your application will not know when the page is loaded for the first time and when it is reloaded.

Also it is better to create dynamic controls in Page_PreInit event. You can also create controls on Page_Load but keep in mind that Page_Load is fired after View State is loaded
0
 

Author Closing Comment

by:murbro
ID: 41853719
Thanks very much
0
 
LVL 35

Expert Comment

by:YZlat
ID: 41853927
Glad i could help
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Setting location of a form 4 22
VB.net PaintEventArgs Handling. 10 28
Form design in vb.net 7 21
Set form below another form 3 26
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

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

12 Experts available now in Live!

Get 1:1 Help Now