Asp.net - Add blank item at top of dropdownlist

asp.netDrop Down-Menu

asp.net Problem Overview


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?

asp.net Solutions


Solution 1 - asp.net

After your databind:

drpList.Items.Insert(0, new ListItem(String.Empty, String.Empty));
drpList.SelectedIndex = 0;

Solution 2 - asp.net

You can use AppendDataBoundItems=true to easily add:

<asp:DropDownList ID="drpList" AppendDataBoundItems="true" runat="server">
	<asp:ListItem Text="" Value="" />
</asp:DropDownList>

Solution 3 - asp.net

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

Solution 4 - asp.net

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();
}

Solution 5 - asp.net

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"

Solution 6 - asp.net

Do your databinding and then add the following:

Dim liFirst As New ListItem("", "")
drpList.Items.Insert(0, liFirst)

Solution 7 - asp.net

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

Solution 8 - asp.net

simple

at last

ddlProducer.Items.Insert(0, "");

Solution 9 - asp.net

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)

Solution 10 - asp.net

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaif KhanView Question on Stackoverflow
Solution 1 - asp.netJasonSView Answer on Stackoverflow
Solution 2 - asp.netayhtutView Answer on Stackoverflow
Solution 3 - asp.netWhiskView Answer on Stackoverflow
Solution 4 - asp.netAndy McCluggageView Answer on Stackoverflow
Solution 5 - asp.netuser492376View Answer on Stackoverflow
Solution 6 - asp.netDillie-OView Answer on Stackoverflow
Solution 7 - asp.netSteven A. LoweView Answer on Stackoverflow
Solution 8 - asp.netUmeshView Answer on Stackoverflow
Solution 9 - asp.netChưa biếtView Answer on Stackoverflow
Solution 10 - asp.netBitsAndBytesView Answer on Stackoverflow