135

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?

kapa
  • 77,694
  • 21
  • 158
  • 175
Saif Khan
  • 18,402
  • 29
  • 102
  • 147
  • 1
    This relates to: http://stackoverflow.com/questions/983716/asp-net-dropdownlist-add-blank-line-before-db-values – mcfea Apr 16 '14 at 23:19

10 Answers10

284

After your databind:

drpList.Items.Insert(0, new ListItem(String.Empty, String.Empty));
drpList.SelectedIndex = 0;
Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
JasonS
  • 23,480
  • 9
  • 41
  • 46
  • Alternately, you can instantiate a ListItem, set its Selected property to true, and then insert it into drpList as above. – nikodaemus Jun 15 '15 at 16:22
  • 3
    *This may help those looking for an answer to work with sqlDataSource data* In my case, I also had to add drpList.AppendDataBoundItems = true; to bind it to the current data in the Page_Load method – sabastienfyrre Sep 14 '15 at 14:47
34

You can use AppendDataBoundItems=true to easily add:

<asp:DropDownList ID="drpList" AppendDataBoundItems="true" runat="server">
    <asp:ListItem Text="" Value="" />
</asp:DropDownList>
Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
ayhtut
  • 384
  • 3
  • 2
25

The databinding takes place after you've added your blank list item, and it replaces what's there already, you need to add the blank item to the beginning of the List from your controller, or add it after databinding.

EDIT:

After googling this quickly as of ASP.Net 2.0 there's an "AppendDataBoundItems" true property that you can set to...append the databound items.

for details see

http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=281 or

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.appenddatabounditems.aspx

Whisk
  • 3,287
  • 2
  • 30
  • 30
13

I think a better way is to insert the blank item first, then bind the data just as you have been doing. However you need to set the AppendDataBoundItems property of the list control.

We use the following method to bind any data source to any list control...

public static void BindList(ListControl list, IEnumerable datasource, string valueName, string textName)
{
    list.Items.Clear();
    list.Items.Add("", "");
    list.AppendDataBoundItems = true;
    list.DataValueField = valueName;
    list.DataTextField = textName;
    list.DataSource = datasource;
    list.DataBind();
}
Andy McCluggage
  • 37,618
  • 18
  • 59
  • 69
9

Like "Whisk" Said, the trick is in "AppendDataBoundItems" property

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DropDownList1.AppendDataBoundItems = true;
        DropDownList1.Items.Insert(0, new ListItem(String.Empty, String.Empty));
        DropDownList1.SelectedIndex = 0;
    }
}

Thanks "Whisk"

5

Do your databinding and then add the following:

Dim liFirst As New ListItem("", "")
drpList.Items.Insert(0, liFirst)
Dillie-O
  • 29,277
  • 14
  • 101
  • 140
3

simple

at last

ddlProducer.Items.Insert(0, "");
arrowd
  • 33,231
  • 8
  • 79
  • 110
Umesh
  • 31
  • 1
3

it looks like you are adding a blank item, and then databinding, which would empty the list; try inserting the blank item after databinding

Steven A. Lowe
  • 60,273
  • 18
  • 132
  • 202
1

ddlCategory.DataSource = ds;
ddlCategory.DataTextField = "CatName";
ddlCategory.DataValueField = "CatID";

Cách 1:

ddlCategory.Items.Add(new ListItem("--please select--", "-1"));
ddlCategory.AppendDataBoundItems = true;
ddlCategory.SelectedIndex = -1;

ddlCategory.DataBind();

Cách 2:

ddlCategory.Items.Insert(0, new ListItem("-- please select --", "0"));

(Tested OK)

Chưa biết
  • 919
  • 8
  • 6
0

You could also have a union of the blank select with the select that has content:

select '' value, '' name
union
select value, name from mytable
Undo
  • 25,519
  • 37
  • 106
  • 129
BitsAndBytes
  • 815
  • 1
  • 9
  • 13
  • Looking back, I wouldn't recommend this method except only as an alternative. I like the event for flexibility sake (OnDataBound="mydropdown_DataBound"), but in my current case, I'm adopting the (AppendDataBoundItems="true") for simplicity sake. – BitsAndBytes Mar 03 '15 at 14:55