Home All Groups Group Topic Archive Search About

Default Value in on Databound Drop Down list

Author
5 Nov 2007 8:28 PM
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.

Author
6 Nov 2007 1:36 AM
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.
--
Nathan Sokalski
njsokal***@hotmail.com
http://www.nathansokalski.com/

Show quote
"Randy Galliano" <randy.galli***@thegallianos.com> wrote in message
news:ukWS3p%23HIHA.1324@TK2MSFTNGP06.phx.gbl...
> 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.
Author
7 Nov 2007 4:30 AM
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.


Nathan Sokalski wrote:
Show quote
> 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.

AddThis Social Bookmark Button