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
Solved

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

Posted on 2016-10-20
8
52 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

861 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