views:

11662

answers:

6

Hi, 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?

+2  A: 

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
when I do that, the blank row appears at the bottom.
Saif Khan
You can specify the index to insert into. Look at JasonS' solution.
SauceMaster
@[Saif Khan]: and if that doesn't work, insert the blank row into your datasource, then databind
Steven A. Lowe
+6  A: 

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
I tried setting this already to true, but doesn't work.
Saif Khan
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.
Keith Sirmons
Is the blank item not there, or there but just not selected?
Whisk
This would work if you had the blank one in your .aspx markup, then bound in code behind.
John Sheehan
+4  A: 

Do your databinding and then add the following:

Dim liFirst As New ListItem("", "")
drpList.Items.Insert(0, liFirst)
Dillie-O
+19  A: 

After your databind:

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

drpList.SelectedIndex = 0;

JasonS
+2  A: 

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
Sorry for the C# code to a VB question
Andy McCluggage
A: 

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"

Mohammed El-Said