Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net - Add blank item at top of dropdownlist

Why is the dropdown not showing my blank item first? Here is what I have

drpList.Items.Add(New ListItem("", ""))

With drpList
    .DataSource = myController.GetList(userid)
    .DataTextField = "Name"
    .DataValueField = "ID"
    .DataBind()
End With

Edit ~ I am binding to a Generig List, could this be the culprit?

like image 539
Saif Khan Avatar asked Oct 04 '22 06:10

Saif Khan


People also ask

How do you make a dropdown empty?

best way is to use $("#dropdownid"). html(''); it makes the value null. This is the only answer here that works when the select does not have a blank option, and in cases where such an option is not desired. The only other way is to add the blank option, as Andy did.


1 Answers

After your databind:

drpList.Items.Insert(0, new ListItem(String.Empty, String.Empty));
drpList.SelectedIndex = 0;
like image 281
JasonS Avatar answered Oct 22 '22 12:10

JasonS