Avatar of kinton
kinton
 asked on

Display selected items of a dynamically created checkboxlist

hi,
was wondering if anyone could help me with displaying the selected items from a dynamically created CheckBoxList. I'm using visual studio 2008 and coding in vb.net.
I was ok when the checkbox list wasn't dynamically created....
any pointers would be greatly appreciated!
cheers!
.NET ProgrammingASP.NETVisual Basic.NET

Avatar of undefined
Last Comment
kinton

8/22/2022 - Mon
Dirk Haest

I think that you'll have to search your control first, and than use that control

Dim control As Control
For Each control In Me.Controls
    If TypeOf (control) Is CheckBoxList And Control.Name = "Yourdynamicnamehere" Then
    Dim myCheckBoxList  As CheckBoxList = control
     If (myCheckBoxList .SelectedItem <> "") Then
            Dim Entry As Object
            For Each Entry In myCheckBoxList .CheckedItems
                MessageBox.Show(Entry.ToString())
            Next
        Else
            MessageBox.Show("You must select an item")
        End If      
   end if
next 

Open in new window

Dirk Haest

For asp.net: http://www.daniweb.com/forums/thread33854.html
Dim chkSelect As checkbox
 For i = 0 To dg.Items.Count - 1
                chkSelect = Me.dg.Items(i).FindControl("chkSelect")
                If chkSelect.Checked Then
                     _doStuff() 
                else
                     _doOtherStuff
                end if
 next i'dg = datagrid
Dim chkSelect As <strong class="highlight">checkbox</strong>
 For i = 0 To dg.Items.Count - 1
                chkSelect = Me.dg.Items(i).FindControl("chkSelect")
                If chkSelect.Checked Then
                     _doStuff() 
                else
                     _doOtherStuff
                end if
 next i

Open in new window

kinton

ASKER
thanks for your post!
Im not sure i understand however?
i have altered my code as such....
-------------------------------------------------------------------------------------------------
Dim control As Control
        For Each control In Me.Controls
            If TypeOf (control) Is CheckBoxList = "CheckBoxListSecondary2" Then
                Dim myCheckBoxList As CheckBoxList = CheckBoxListSecondary2
                If (myCheckBoxList.SelectedItem.Value <> "") Then
                    Dim Entry As Object
                    For Each Entry In myCheckBoxList.SelectedItem.Value
                        lblShow2.Text = (Entry.ToString())
                    Next
                Else
                    lblShow2.Text = ("You must select an item")
                End If
            End If
        Next
-----------------------------------------------------------------------------------------------------------------------
CheckBoxListSecondary2 is my dynamically created checkbox list, and lblShow is the label...
however when i run this im getting an error saying that the input string was not in correct format?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Dirk Haest

Did you try this:
Dim chkSelect As checkbox
 For i = 0 To dg.Items.Count - 1
                chkSelect = Me.dg.Items(i).FindControl("CheckBoxListSecondary2")
                If chkSelect.Checked Then
                     lblShow2.Text = chkSelect.SelectedText
                else
                     _doOtherStuff
                end if
 next i

Open in new window

kinton

ASKER
Im playing around with these snippets of code you gave me but still having no joy!
im very much a novice programmer, so if there is any explanation perhaps in lehmann's terms it would be very helpful!
many thanks again!
ASKER CERTIFIED SOLUTION
Dirk Haest

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
kinton

ASKER
Thats great, i'll have a read.
Thanks very much for your help!!!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Dirk Haest

Selected values from a dynamically created CheckBoxList
http://forums.asp.net/t/1024509.aspx
foreach (ListItem item in ((CheckBoxList)Form.FindControl("cblActivities")).Items)
{
if (item.Selected)
{
string Activities += item.Value; 
 
}
}
Response.Write(Activities);

Open in new window

kinton

ASKER
Ive found the problem!
The code could not loop as it was in the page load event, for some reason!!
when i moved it to the page Init event it works fine!!
thank you for your help again!