DataBound dropdownlist + extra Item

G

Guest

Hi guys

I've got a dropdownlist which is being databound to an sqldatareader at
runtime. I want to add an extra Item to the list, not from the database.

I know I can do this with

ddl1.Items.Add(new ListItem("text", "value"));

works fine. However, if I put this above my DataBinding code, it doesn't
appear. After it it appears at the end of the list fine.

Question is, how do I databind AND get this item to the top of my list? It's
an ALL option y'see.

Cheers for any help!


Dan
 
S

Simon Hazelton

ddlLocation.DataSource = Line.GetBrandingLocations

ddlLocation.DataValueField = "BrandingLocationID"

ddlLocation.DataTextField = "BrandingLocation"

ddlLocation.DataBind()

'the following line inserts at position 0

ddlLocation.Items.Insert(0, "(Select)")
 
G

Guest

Thanks SImon, worked great.


Dan

Simon Hazelton said:
ddlLocation.DataSource = Line.GetBrandingLocations

ddlLocation.DataValueField = "BrandingLocationID"

ddlLocation.DataTextField = "BrandingLocation"

ddlLocation.DataBind()

'the following line inserts at position 0

ddlLocation.Items.Insert(0, "(Select)")
 
E

Eliyahu Goldin

Dan,

Do the databinding first and then call

ddl1.Items.Insert(0, new ListItem("text", "value"));

Eliyahu
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top