Inserting a default ListItem in DropDownList Control

G

Guest

I want to insert a default null ListItem with the text "choose an item ..."
in my DropDownList control. I have seen similar postings here regarding this
issue but all of them seem to focus around having to populate datasources,
datatables etc. and then bind the DropDownList in the codebehind of the .aspx
page. I am using Visual Studio 2005 and the .NET Framework 2.0. I thought
that with all these new improvements we could avoid all of the irritations of
..NET Framework 1.x and the weak controls that came with the IDE by being able
to automate all of this databinding stuff. It seems like the only way to
insert individual ListItems is if you go back to doing everything in code.
What gives?

Thanks in advance.
 
J

Joey

[...databinding code...]

ListItem Choose = new ListItem("Choose..."," ");
this.ddlMyDropDownList.Items.Insert(0,Choose);

The 0 in Items.Insert is the item index in the dropdown list. Also, you
must use a space within the two quotes for it to work properly.

The above will create a "Choose..." at the top of your databound
drop-down list (which is great as long as you databound only for
purposes of populating the list).

I prefer to create a blank one instead...new ListItem(" "," ")...and
then use client-side
javascript...document.all['ddlMyDropDownList'].selectedIndex != 0...to
validate that something has been chosen.
 

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

Latest Threads

Top