Question

I am using a drop down server control that is databound to a data source, but I would also like to have a default value ("Select a program") that is hard-coded as the first item of the list.

Code-behind

/// <summary>
/// Page Load
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
    ddPrograms.DataSource = Programs.SelectProgramID(1);
    ddPrograms.DataBind();        
}

Front-end

  <asp:DropDownList ID="ddPrograms" runat="server" DataValueField="ProgramID" DataTextField="Name" AutoPostBack="true">
            <asp:ListItem Text="Select a program" Value="0" Selected="True"></asp:ListItem>   
        </asp:DropDownList>
Was it helpful?

Solution

Try setting AppendDataBoundItems="true" on the DropDownList.

<asp:DropDownList ID="ddPrograms" runat="server" DataValueField="ProgramID" DataTextField="Name" AutoPostBack="true" AppendDataBoundItems="true">
        <asp:ListItem Text="Select a program" Value="0" Selected="True"></asp:ListItem>   
    </asp:DropDownList>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top