472,139 Members | 1,366 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,139 software developers and data experts.

add items to listbox inside a codebehind page returns error - Type 'ListItem' is not defined.

Hi,

I'm trying to add a default item to my listbox but when I do it tells me
that it's not defined, could someone tell me the syntax I need to use to get
the listbox control to display a default item?

I have tried the following:

lstStates.Items.Insert(0, new ListItem("By State","0"))

ME.lstStates.Items.Insert(0, new ListItem("By State","0"))

Joey

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
CategoryID = Cint(request.querystring("CategoryID"))

If Not Page.IsPostBack() Then

lstStates.DataSource = BindStates

lstStates.DataValueField = "LocationID"

lstStates.DataTextField = "Location"

' lstStates.Items.Insert(0, new ListItem("By State","0"))

lstStates.SelectedIndex = 0

lstStates.DataBind()
lstCategorie.DataSource = BindCategories

lstCategorie.DataValueField = "CategoryID"

lstCategorie.DataTextField = "CategoryDesc"

'lstCategorie.Items.Insert(0, new ListItem("By Category","0"))

lstCategorie.SelectedIndex = 0

lstCategorie.DataBind()
BindTheData(CategoryID)

End If

End Sub
Nov 18 '05 #1
3 4689
Hi Ryan,

The error happens when the page is loaded, I have the list on the page and
the listbox declared in the page. The error says Type 'ListItem' is not
defined..

What am I misssing?
Protected WithEvents lstStates As System.Web.UI.WebControls.ListBox

Protected WithEvents lstCategorie As System.Web.UI.WebControls.ListBox

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
CategoryID = Cint(request.querystring("CategoryID"))

If Not Page.IsPostBack() Then

lstStates.DataSource = BindStates

lstStates.DataValueField = "LocationID"

lstStates.DataTextField = "Location"

' lstStates.Items.Insert(0, new ListItem("By State","0"))

lstStates.SelectedIndex = 0

lstStates.DataBind()
lstCategorie.DataSource = BindCategories

lstCategorie.DataValueField = "CategoryID"

lstCategorie.DataTextField = "CategoryDesc"

lstCategorie.Items.Insert(0, new ListItem("By Category","0"))

lstCategorie.SelectedIndex = 0

lstCategorie.DataBind()
BindTheData(CategoryID)

End If

End Sub

"Ryan Riddell" <Ry*********@discussions.microsoft.com> wrote in message
news:C1**********************************@microsof t.com...
It looks like everything is working just fine. I did the following:
LB.Items.Add(New ListItem("Field03 - Field04", "Field03 - Field04"))
LB.Items.Add(New ListItem("Field05 - Field06", "Field05 - Field06"))
LB.Items.Insert(0, New ListItem("RYAN"))

And it worked just fine. What line is the error on? What "isn't defined"? You do have the listbox defined on the .aspx page right? Or at
least added to the page at some point?
-Ryan

"Joey" wrote:
Hi,

I'm trying to add a default item to my listbox but when I do it tells me
that it's not defined, could someone tell me the syntax I need to use to get the listbox control to display a default item?

I have tried the following:

lstStates.Items.Insert(0, new ListItem("By State","0"))

ME.lstStates.Items.Insert(0, new ListItem("By State","0"))

Joey

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
CategoryID = Cint(request.querystring("CategoryID"))

If Not Page.IsPostBack() Then

lstStates.DataSource = BindStates

lstStates.DataValueField = "LocationID"

lstStates.DataTextField = "Location"

' lstStates.Items.Insert(0, new ListItem("By State","0"))

lstStates.SelectedIndex = 0

lstStates.DataBind()
lstCategorie.DataSource = BindCategories

lstCategorie.DataValueField = "CategoryID"

lstCategorie.DataTextField = "CategoryDesc"

'lstCategorie.Items.Insert(0, new ListItem("By Category","0"))

lstCategorie.SelectedIndex = 0

lstCategorie.DataBind()
BindTheData(CategoryID)

End If

End Sub

Nov 18 '05 #2
Are you sure you have a reference System.Web.UI.WebControls? I notice that
you're using the fully qualified name on your listbox declarations but not
on the ListItem declarations.

Dale Preston
MCAD, MCSE, MCDBA

"Joey" <jo**********@digital.com> wrote in message
news:up**************@tk2msftngp13.phx.gbl...
Hi Ryan,

The error happens when the page is loaded, I have the list on the page and
the listbox declared in the page. The error says Type 'ListItem' is not
defined..

What am I misssing?
Protected WithEvents lstStates As System.Web.UI.WebControls.ListBox

Protected WithEvents lstCategorie As System.Web.UI.WebControls.ListBox

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
CategoryID = Cint(request.querystring("CategoryID"))

If Not Page.IsPostBack() Then

lstStates.DataSource = BindStates

lstStates.DataValueField = "LocationID"

lstStates.DataTextField = "Location"

' lstStates.Items.Insert(0, new ListItem("By State","0"))

lstStates.SelectedIndex = 0

lstStates.DataBind()
lstCategorie.DataSource = BindCategories

lstCategorie.DataValueField = "CategoryID"

lstCategorie.DataTextField = "CategoryDesc"

lstCategorie.Items.Insert(0, new ListItem("By Category","0"))

lstCategorie.SelectedIndex = 0

lstCategorie.DataBind()
BindTheData(CategoryID)

End If

End Sub

"Ryan Riddell" <Ry*********@discussions.microsoft.com> wrote in message
news:C1**********************************@microsof t.com...
It looks like everything is working just fine. I did the following:
LB.Items.Add(New ListItem("Field03 - Field04", "Field03 - Field04"))
LB.Items.Add(New ListItem("Field05 - Field06", "Field05 - Field06"))
LB.Items.Insert(0, New ListItem("RYAN"))

And it worked just fine. What line is the error on? What "isn't defined"? You do have the listbox defined on the .aspx page right? Or at
least added to the page at some point?

-Ryan

"Joey" wrote:
Hi,

I'm trying to add a default item to my listbox but when I do it tells me that it's not defined, could someone tell me the syntax I need to use
to get the listbox control to display a default item?

I have tried the following:

lstStates.Items.Insert(0, new ListItem("By State","0"))

ME.lstStates.Items.Insert(0, new ListItem("By State","0"))

Joey

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
CategoryID = Cint(request.querystring("CategoryID"))

If Not Page.IsPostBack() Then

lstStates.DataSource = BindStates

lstStates.DataValueField = "LocationID"

lstStates.DataTextField = "Location"

' lstStates.Items.Insert(0, new ListItem("By State","0"))

lstStates.SelectedIndex = 0

lstStates.DataBind()
lstCategorie.DataSource = BindCategories

lstCategorie.DataValueField = "CategoryID"

lstCategorie.DataTextField = "CategoryDesc"

'lstCategorie.Items.Insert(0, new ListItem("By Category","0"))

lstCategorie.SelectedIndex = 0

lstCategorie.DataBind()
BindTheData(CategoryID)

End If

End Sub


Nov 18 '05 #3
Sorry for the delay, I couldn't post yesterday for no apparent reason.

Try what Dale said. If that doesn't work, try doing this order:

lstCategorie.DataBind()
lstCategorie.Items.Insert(0, new ListItem("By Category","0"))
lstCategorie.SelectedIndex = 0

That is bind the data, then try and add the new ListItem.

"Joey" wrote:
Hi Ryan,

The error happens when the page is loaded, I have the list on the page and
the listbox declared in the page. The error says Type 'ListItem' is not
defined..

What am I misssing?
Protected WithEvents lstStates As System.Web.UI.WebControls.ListBox

Protected WithEvents lstCategorie As System.Web.UI.WebControls.ListBox

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
CategoryID = Cint(request.querystring("CategoryID"))

If Not Page.IsPostBack() Then

lstStates.DataSource = BindStates

lstStates.DataValueField = "LocationID"

lstStates.DataTextField = "Location"

' lstStates.Items.Insert(0, new ListItem("By State","0"))

lstStates.SelectedIndex = 0

lstStates.DataBind()
lstCategorie.DataSource = BindCategories

lstCategorie.DataValueField = "CategoryID"

lstCategorie.DataTextField = "CategoryDesc"

lstCategorie.Items.Insert(0, new ListItem("By Category","0"))

lstCategorie.SelectedIndex = 0

lstCategorie.DataBind()
BindTheData(CategoryID)

End If

End Sub

"Ryan Riddell" <Ry*********@discussions.microsoft.com> wrote in message
news:C1**********************************@microsof t.com...
It looks like everything is working just fine. I did the following:
LB.Items.Add(New ListItem("Field03 - Field04", "Field03 - Field04"))
LB.Items.Add(New ListItem("Field05 - Field06", "Field05 - Field06"))
LB.Items.Insert(0, New ListItem("RYAN"))

And it worked just fine. What line is the error on? What "isn't

defined"? You do have the listbox defined on the .aspx page right? Or at
least added to the page at some point?

-Ryan

"Joey" wrote:
Hi,

I'm trying to add a default item to my listbox but when I do it tells me
that it's not defined, could someone tell me the syntax I need to use to get the listbox control to display a default item?

I have tried the following:

lstStates.Items.Insert(0, new ListItem("By State","0"))

ME.lstStates.Items.Insert(0, new ListItem("By State","0"))

Joey

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
CategoryID = Cint(request.querystring("CategoryID"))

If Not Page.IsPostBack() Then

lstStates.DataSource = BindStates

lstStates.DataValueField = "LocationID"

lstStates.DataTextField = "Location"

' lstStates.Items.Insert(0, new ListItem("By State","0"))

lstStates.SelectedIndex = 0

lstStates.DataBind()
lstCategorie.DataSource = BindCategories

lstCategorie.DataValueField = "CategoryID"

lstCategorie.DataTextField = "CategoryDesc"

'lstCategorie.Items.Insert(0, new ListItem("By Category","0"))

lstCategorie.SelectedIndex = 0

lstCategorie.DataBind()
BindTheData(CategoryID)

End If

End Sub


Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chuck Haeberle | last post: by
2 posts views Thread by TJS | last post: by
reply views Thread by Fred Blum | last post: by
1 post views Thread by zoneal | last post: by
reply views Thread by SimonZ | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.