Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Default Value in on Databound Drop Down list

Reply
Thread Tools

Default Value in on Databound Drop Down list

 
 
Randy Galliano
Guest
Posts: n/a
 
      11-05-2007
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.
 
Reply With Quote
 
 
 
 
Nathan Sokalski
Guest
Posts: n/a
 
      11-06-2007
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.nathansokalski.com/

"Randy Galliano" <(E-Mail Removed)> wrote in message
news:ukWS3p%(E-Mail Removed)...
> 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.



 
Reply With Quote
 
 
 
 
Randy Galliano
Guest
Posts: n/a
 
      11-07-2007
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:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
databound Drop Down List not changing on new selection KROSS General Computer Support 0 01-21-2008 04:48 PM
Drop down list error: "Databinding methods such as Eval(), XPath(),and Bind() can only be used in the context of a databound control." yosri2005@gmail.com ASP .Net 0 12-13-2007 02:00 PM
DataBound Drop Down List thebison ASP .Net 1 07-16-2007 10:30 PM
retrive preselected value in second drop down list from the first drop down list weiwei ASP .Net 0 01-05-2007 07:29 PM
Auto Drop down a Drop Down List xxbmichae1@supergambler.com Javascript 5 11-23-2005 01:35 AM



Advertisments