How to add a default “Select” option to this ASP.NET DropDownList control?

后端 未结 7 2085
别那么骄傲
别那么骄傲 2021-01-02 05:06

I am a new ASP.NET developer and I am trying to learn Linq-To-Entities. I am trying to bind a DropDownList with the Linq statement for retrieving the list of status in the S

7条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-02 05:19

    The reason it is not working is because you are adding an item to the list and then overriding the whole list with a new DataSource which will clear and re-populate your list, losing the first manually added item.

    So, you need to do this in reverse like this:

    Status status = new Status();
    DropDownList1.DataSource = status.getData();
    DropDownList1.DataValueField = "ID";
    DropDownList1.DataTextField = "Description";
    DropDownList1.DataBind();
    
    // Then add your first item
    DropDownList1.Items.Insert(0, "Select");
    

提交回复
热议问题