Simple DropDownList/ComboBox question

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I'm databinding my DropDownList to a DataSet. How do I add a dummy record
at the top of the combo box.

In other words, suppose we are dealing with cars I want something like:

<select name="cboCars">
<option value="0">Select a car</option>
<option value="1">Ford Mustang</option>
....
<option value="5">Mitsubishi Eclipse</option>
</select>

How do I get the "Select a car" option at the top, while still databinding
the DropDownList?

If I don't have the dummy record the DropDownList auto selects the first
option as the default, which isn't good for my purpose.

Thanks in advance,
Dan
 
*After* you databind, do a dropdownlist.items.add(0, "")

HTH,

Morgan
 
This is assuming you are binding via code i.e. dropdownlist.datasource =
mydataset.tables(0)
I've never done visual databinding in the WebForm designer, but you should
be able to add the dropdownlist.items.insert(0, "") (sorry, chose the wrong
method from memory) on the page_load event.

--Morgan
 
Worked like a charm. I knew it had to be something simple that I was
missing.

Thanks,
Dan
 
after you bound the DropDownList
call:
ddl.Items.Insert(0,new ListItem("Select a car","0"));
 

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

Back
Top