2014-04-27 252 views
0

我有一個從下拉列表中選擇的名稱列表。名稱正在從數據源中取出,而不是從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> 

回答

0

嘗試添加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> 
+0

感謝這個工作 – user3281606