質問

I have a list of names to select from a dropdown list. The names are being pulled from a data source and not from a ListItem. I want to leave the box blank while no selection has been. How can I do this?

<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>
役に立ちましたか?

解決

Try add AppendDataBoundItems="True" to the tag and add <asp:ListItem Selected="True"></asp:ListItem> as an item

<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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top