下拉列表中的默认选择

本文关键字:选择 默认 下拉列表 | 更新日期: 2023-09-27 18:00:37

我有一个从下拉列表中选择的名称列表。这些名称是从数据源而非ListItem中提取的。我想在没有选择的情况下将框留空。我该怎么做?

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
    DataSourceID="SqlDataSource2" DataTextField="director" 
    DataValueField="director">
    <!--<asp:ListItem Text="Please Select Director" Value="-1"/>-->
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ 
    ConnectionStrings:dvdsConnectionString %>" ProviderName="<%$ 
    ConnectionStrings:dvdsConnectionString.ProviderName %>" 
    SelectCommand="SELECT [director] FROM [dvds]">
</asp:SqlDataSource>

下拉列表中的默认选择

尝试将AppendDataBoundItems="True"添加到标签并将<asp:ListItem Selected="True"></asp:ListItem>添加为项目

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
    DataSourceID="SqlDataSource2" DataTextField="director" 
    DataValueField="director" AppendDataBoundItems="True">
    <asp:ListItem Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ 
    ConnectionStrings:dvdsConnectionString %>" ProviderName="<%$ 
    ConnectionStrings:dvdsConnectionString.ProviderName %>" 
    SelectCommand="SELECT [director] FROM [dvds]">
</asp:SqlDataSource>