Asp.net - Add blank item at top of dropdownlist

后端 未结 10 1810
甜味超标
甜味超标 2020-11-30 20:03

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

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

With drpList
    .DataSource = myContro         


        
10条回答
  •  伪装坚强ぢ
    2020-11-30 20:27

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

提交回复
热议问题