asp:dropdownlist

Ø

Øyvind Isaksen

When I bind the dropdownlist-control to a datareader, it displays all the
data (from the database) in the dropdownlist.

My question is:
How do I insert a "<option value="">Choose...</option>" in the top of the
dropdownlist? I want the first choice in the dropdownlist to be "blank" (no
value), just display a text like "Choose"...


This is the code that bind the dropdownlist to the datareader:

cmd.Connection.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader
Me.drpCompany.DataSource = dr
Me.drpCompany.DataTextField = "company"
Me.drpCompany.DataValueField = "companyID"
Me.drpCompany.DataBind()
cmd.Connection.Close()


Thanks for all tips :)
 
K

Kim Quigley

I didn't test this, but I'm pretty sure this is what you have to do. After
the DataBind line of code, add:

Me.drpCompany.items.insert(0,"")
 
Ø

Øyvind Isaksen

Yes, this inserted a blank in the top of the dropdownlist.
But, I only want the value to be blank (""), now the value AND text is
blank. Do you know how I get the text = "choose"...? (And still value = "")

Thanks!!
 
Z

Zamdrist

The only way to get it to display "Choose..." is to add it as Kim said,
but changing "" to "Choose...".

I would suggest using a label beside the dropdownlist.
 
E

Ed

You can *manually* add the first item to your DropDownList, then
programmatically add more listitems.

Ex:
DropDownList.Items.Add(blah - data from your db)
 
Ø

Øyvind Isaksen

I got the solution, this works:
me.myDromDovn.Items.Insert(0, new ListItem("Choose ...", string.Empty))

Øyvind Isaksen :)




The only way to get it to display "Choose..." is to add it as Kim said,
but changing "" to "Choose...".

I would suggest using a label beside the dropdownlist.
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top