Solved

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

Posted on 2016-10-20
8
44 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 34

Expert Comment

by:Paul MacDonald
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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 …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

15 Experts available now in Live!

Get 1:1 Help Now