asp.net dropdownlist - add blank line before db values asp.net dropdownlist - add blank line before db values asp.net asp.net

asp.net dropdownlist - add blank line before db values


You can simply add a ListItem inside the DropDownList Markup. All the values from the DataSource will be appended after that.

<asp:DropDownList ID="drpClient" runat="server" Width="200px"           AutoPostBack="True" DataSourceID="dsClients" DataTextField="name"           DataValueField="client_id" AppendDataBoundItems="true">   <asp:ListItem>-- pick one --</asp:ListItem></asp:DropDownList>


<asp:DropDownList ID="drpClient" runat="server" Width="200px"         AutoPostBack="True" DataSourceID="dsClients" DataTextField="name"         DataValueField="client_id" AppendDataBoundItems="True">    <asp:ListItem Text="" Value="" /> </asp:DropDownList>

It's easy to miss, so don't forget the AppendDataBoundItems attribute I added.


I haven't really tested this but I'm assuming that you could add an item after you have binded the the drop down list. You could add this event to any dropdown list you'd like to add this empty box to.

protected void DropDownList_DataBound(object sender, EventArgs e)    {        DropDownList ddl = (DropDownList)sender;        ListItem emptyItem = new ListItem("", "");        ddl.Items.Insert(0, emptyItem);    }