By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,718 Members | 1,840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,718 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.