未定义类型"ListItem". [英] Type 'ListItem' is not defined.

查看:214
本文介绍了未定义类型"ListItem".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

For Each item As ListItem In ListBox1.Items
            If item.Selected Then
                Try
                    cmd.Parameters("@coursename").Value = item.Text
                    cmd.ExecuteNonQuery()
                Catch ex As Exception
                    Label1.Text = ex.Message
                End Try
            End If
        Next

推荐答案

只需将代码替换为:
Just replace your code with:
Dim oitem as Object = Nothing

For Each oitem In ListBox1.Items
    MsgBox(oitem.ToString())
Next



有关更多信息,请访问: http://msdn.microsoft.com/zh-CN -us/library/system.windows.forms.listbox.items.aspx#Y570 [



More at: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.items.aspx#Y570[^]


问题是not tagged with the UI type.

我认为UI type may be Windows Forms,因为ASP.NET ListBox控件的Items 属性返回System.Web.UI.WebControls.ListItemCollection
如此处
http://msdn所述. microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.items(v=vs.90) [ http://msdn.microsoft所述. com/en-us/library/system.web.ui.webcontrols.listitemcollection(v = vs.90) [ http://stackoverflow.com/questions/873944/asp-net-display从列表中选择项目ox-in-textbox [ ^ ]而不会产生错误.

对于Windows Forms UI typeListBox 控件的Items 属性返回System.Windows.Forms.ListBox.ObjectCollection ,如此处 http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditems [
The question is not tagged with the UI type.

I think the UI type may be Windows Forms, since the Items property of ASP.NET ListBox control returns System.Web.UI.WebControls.ListItemCollection
as explained here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.items(v=vs.90)[^], which is a collection of ListItem objects as explained here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitemcollection(v=vs.90)[^] and they can be iterated using ListItem as shown here http://stackoverflow.com/questions/873944/asp-net-display-selected-items-from-listbox-in-textbox[^] without generating the error.

In case of Windows Forms UI type, the Items property of ListBox control returns System.Windows.Forms.ListBox.ObjectCollection as explained here http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.items[^], the elements of which are not of ListItem type and this may be the reason for the above error.

If the requirement is to get only the selected items, then I think the SelectedItems property explained here http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditems[^] can be used for this purpose as shown below:
For Each item As Object In ListBox1.SelectedItems
    Try
        cmd.Parameters("@coursename").Value = item.ToString()
        cmd.ExecuteNonQuery()
    Catch ex As Exception
        Label1.Text = ex.Message
    End Try
Next


请参阅此网站
http://social.msdn.microsoft.com/Forums/否/vbgeneral/thread/355eb437-d66c-4310-87ea-0b4ba05e0acb [
Refer this site
http://social.msdn.microsoft.com/Forums/no/vbgeneral/thread/355eb437-d66c-4310-87ea-0b4ba05e0acb[^]


这篇关于未定义类型"ListItem".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆