【问题标题】:Problem while trying to retrieve data from checkbox尝试从复选框中检索数据时出现问题
【发布时间】:2010-11-15 11:09:49
【问题描述】:

我正在尝试执行下面的代码以在复选框中列出选择项

到邮件正文

 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

但我收到以下错误..

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 

请帮忙

【问题讨论】:

  • 您能发布 HTML 以显示复选框的包装内容吗?
  • txt_panview0_ddinput1 是什么类型?
  • txt_panview0_ddinput1 不是一个列表框,它是一个复选框列表——
  • 这不是我们刚刚在另一个问题中为您从 C# 转换而来的代码吗?

标签: asp.net vb.net checkbox


【解决方案1】:

在检查是否已检查之前将迭代中的每个项目类型转换为 CheckBox

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

要启用多个值的选择,请将ListBoxSelectionMode 属性设置为Multiple

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

然后要遍历选定的值,请使用以下命令:

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

PS 我对 VB.Net 语法有点生疏,所以我的代码可能在语法上并不完美

【讨论】:

  • 现在给出:System.InvalidCastException:无法将“System.Web.UI.WebControls.ListItem”类型的对象转换为“System.Web.UI.WebControls.CheckBox”类型。在 C:\UseFormCode\UseFormEnhWorking\Default.aspx.vb:line 271 中的 WebApplication1._Default.collectEmailBodyText() 处
  • 尝试:dim c as CheckBox = Ctype(item.Value, CheckBox)
  • 没有仍然错误:System.InvalidCastException:无法将“System.String”类型的对象转换为“System.Web.UI.WebControls.CheckBox”类型。在 C:\UseFormCode\UseFormEnhWorking\Default.aspx.vb:line 271 中的 WebApplication1._Default.collectEmailBodyText() 处
  • 是的,它的工作,但一个问题只有一个选定的值只是comming而不是多个值:-(
  • 设置ListBox的SelectionMode属性为Multiple
猜你喜欢
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多