Adding Blank Selection To A DropDownList Control

Because when you databind the data to the control, all previous data gets wiped out, so you have to add the new item afterwards. Also, it will appear at the end of the list. I don’t know of a way around that.

This code adds and selects an empty list item:

nameList.DataSource = MyDataTable
nameList.DataTextField = "Name"
nameList.DataValueField = "ID"
nameList.DataBind()
nameList.Items.Add("")
nameList.Items.FindByText("").Selected = True

Tip Submitted By: David McCarter

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s