Solved

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

Posted on 2016-10-20
8
70 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Murray Brown
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:Murray Brown
ID: 41852569
Also. Thanks paulmacd. I did what you said but unfortunately that didn't work
0
 
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:Murray Brown
ID: 41853719
Thanks very much
0
 
LVL 35

Expert Comment

by:YZlat
ID: 41853927
Glad i could help
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

627 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