1

I am trying to execute the code below to list the select item in a checkbox

to the body of the mail

 Dim CheckedValues As String
                For Each item In txt_panview0_ddinput1.Items
                    If item.checked Then
                        checkedValues = checkedValues & item.selectedValue

                    End If
                Next
                If Not String.IsNullOrEmpty(checkedValues) Then
                    checkedValues = checkedValues.Substring(1)
                End If


                tempCollector = tempCollector + "<br>" + "Area Name" + ": " + checkedValues

But i am getting the following error ..

System.MissingMemberException: Public member 'checked' on type 'ListItem' not found. 
at Microsoft.VisualBasic.CompilerServices.Symbols.Container.GetMembers(String& MemberName, 
Boolean ReportErrors) at Microsoft.VisualBasic.CompilerServices.NewLateBinding.
LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] 
TypeArguments, Boolean[] CopyBack) at WebApplication1._Default.collectEmailBodyText() 
in C:\UseFormCode\UseFormEnhWorking\Default.aspx.vb:line 271 

Please help

4
  • Can you post the HTML to show what the check boxes are wrapped in? Commented Jul 30, 2009 at 14:24
  • What type is txt_panview0_ddinput1 ? Commented Jul 30, 2009 at 14:32
  • txt_panview0_ddinput1 is not a listbox it is a checkboxlist – Commented Jul 30, 2009 at 15:07
  • Isn't this the code we just converted from C# for you in another question? Commented Jul 30, 2009 at 16:33

1 Answer 1

1

Typecast each item in the iteration to a CheckBox before checking if it's checked:

For Each item In txt_panview0_ddinput1.Items
     dim c as CheckBox = Ctype(item.Value, CheckBox)
     If c.checked Then
         checkedValues = checkedValues & item.selectedValue
    End If
Next

To enable selection of multiple values, set the SelectionMode property of the ListBox to Multiple :

<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox>

Then to iterate over the selected values, use the following:

For Each item as ListItem In txt_panview0_ddinput1.Items
         If item.Selected Then
             CheckedValues = CheckedValues & item.Value
        End If
Next

PS I'm a bit rusty on VB.Net syntax so my code may not be syntactically perfect

Sign up to request clarification or add additional context in comments.

7 Comments

Now its giving : System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.ListItem' to type 'System.Web.UI.WebControls.CheckBox'. at WebApplication1._Default.collectEmailBodyText() in C:\UseFormCode\UseFormEnhWorking\Default.aspx.vb:line 271
Try : dim c as CheckBox = Ctype(item.Value, CheckBox)
Nope still error: System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Web.UI.WebControls.CheckBox'. at WebApplication1._Default.collectEmailBodyText() in C:\UseFormCode\UseFormEnhWorking\Default.aspx.vb:line 271
yes its worked but one problem only one selected value is only comming not multiple values :-(
set the SelectionMode property of the ListBox to Multiple
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.