Default Value in on Databound Drop Down list

R

Randy Galliano

Hello,

I am populating a drop down list control with a table from a database.
I would like the control to display a value such as --- select --- when
it first comes up, instead of any values from the table. Is there a way
to do this without putting the value --- Select --- in the actual table?

Regards,

Randy.
 
N

Nathan Sokalski

Yes, after calling the Databind() method, use the use the Items.Insert()
method. Here is an example:

MyDropDown.DataBind()
MyDropDown.Items.Insert(0,"--- Select ---")

This inserts an extra ListItem before index 0, therefore changing what was
previously index 0 into index 1, index 1 into index 2, etc. Since this is
only 1 extra line of code, it is very little extra work, and makes it the
first ListItem regardless of what is in the database. Hopefully this helps.
 
R

Randy Galliano

Thank you so much. That worked very well. I put it in the Page_Load
event so I also added a check for ispostback.

Regards,

Randy.
 

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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top