문제

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>
도움이 되었습니까?

해결책

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top