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

J

Joey

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
 
J

Joey

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 said:
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?
 
D

Dale

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top