Solved

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

Posted on 2016-10-20
8
49 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:murbro
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:murbro
ID: 41853719
Thanks very much
0
 
LVL 35

Expert Comment

by:YZlat
ID: 41853927
Glad i could help
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

777 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