Asp.net - Add blank item at top of dropdownlist
Asked Answered
R

10

136

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?

Reconcilable answered 5/11, 2008 at 23:0 Comment(1)
This relates to: #984216Steatopygia
D
285

After your databind:

drpList.Items.Insert(0, new ListItem(String.Empty, String.Empty));
drpList.SelectedIndex = 0;
Doubledecker answered 5/11, 2008 at 23:5 Comment(2)
Alternately, you can instantiate a ListItem, set its Selected property to true, and then insert it into drpList as above.Vick
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 methodSurely
L
34

You can use AppendDataBoundItems=true to easily add:

<asp:DropDownList ID="drpList" AppendDataBoundItems="true" runat="server">
    <asp:ListItem Text="" Value="" />
</asp:DropDownList>
Lynseylynus answered 28/11, 2012 at 2:57 Comment(3)
VS doesn't like the <br /> tag and it works for me without it. This is such a clean option, I don't know why it doesn't have more votes.Sallyanne
Worked like a charm. Thanks for the design time solution.Stegodon
Don't forget to set Selected="true"Sirmons
C
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

Chouinard answered 5/11, 2008 at 23:2 Comment(3)
One think you need to watch out for is having your dropdown list grow after each postback by appending the same data over and over.Weisburgh
Is the blank item not there, or there but just not selected?Chouinard
This would work if you had the blank one in your .aspx markup, then bound in code behind.Neumark
B
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();
}
Briannabrianne answered 18/3, 2009 at 17:36 Comment(0)
K
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"

Knuckle answered 30/10, 2010 at 20:14 Comment(0)
C
5

Do your databinding and then add the following:

Dim liFirst As New ListItem("", "")
drpList.Items.Insert(0, liFirst)
Construct answered 5/11, 2008 at 23:4 Comment(0)
S
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

Staton answered 5/11, 2008 at 23:1 Comment(3)
when I do that, the blank row appears at the bottom.Reconcilable
You can specify the index to insert into. Look at JasonS' solution.Ube
@[Saif Khan]: and if that doesn't work, insert the blank row into your datasource, then databindStaton
S
3

simple

at last

ddlProducer.Items.Insert(0, "");
Sepulchre answered 8/5, 2012 at 12:50 Comment(0)
C
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)

Chuckchuckfull answered 24/4, 2013 at 8:10 Comment(0)
Q
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
Quadrisect answered 2/3, 2015 at 21:53 Comment(1)
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.Quadrisect

© 2022 - 2024 — McMap. All rights reserved.