ASP.NET Web Forms 下拉式選單繫結資料庫並加入額外預設值

筆記下老東西

結論

加上 AppendDataBoundItems="true"

範例

<asp:SqlDataSource ID="ds1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDb %>"
    SelectCommand="SELECT [id], [name] FROM [Test] WHERE ([IsDeleted] = @isDeleted) ORDER BY [seq]">
    <SelectParameters>
        <asp:Parameter DefaultValue="0" Name="isDeleted" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

<asp:DropDownList ID="cbo1" runat="server" DataSourceID="ds1" DataTextField="name" DataValueField="id" AppendDataBoundItems="true">
    <asp:ListItem Text="預設" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>

參考

[程式筆記]下拉選單DropDownList範例(asp.net) @ 貓羽的文字日誌 :: 痞客邦 :: (pixnet.net)

PS5