asp:dropdownlist

  • Thread starter Thread starter Øyvind Isaksen
  • Start date Start date
Ø

Ø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 :)
 
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,"")
 
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!!
 
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.
 
You can *manually* add the first item to your DropDownList, then
programmatically add more listitems.

Ex:
DropDownList.Items.Add(blah - data from your db)
 
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.
 
Back
Top