8

DropDownList を使用する場合:

 <asp:DropDownList ID="DropDownListSubContractors" runat="server" 
     DataTextField="Company_Name" DataValueField="id">
 </asp:DropDownList>

リストの最初の値の代わりに、ドロップダウンの最初のオプションとして「---Select---」を使用できるようにする属性を使用/設定します。

4

3 に答える 3

15

使用できます

<asp:DropDownList ID="DropDownListSubContractors" runat="server" AppendDataBoundItems="true" DataTextField="Company_Name" DataValueField="id">
    <asp:ListItem Text="---Select---" Value="0" />   
</asp:DropDownList>

または、このようにコードビハインドでこれを動的に追加できます

DropDownListSubContractors.Items.Add(new ListItem("---Select---", "0"));
于 2013-02-04T12:37:17.947 に答える
7
<asp:DropDownList ID="DropDownListSubContractors" runat="server" AppendDataBoundItems="true" DataTextField="Company_Name" DataValueField="id">
    <asp:ListItem Text="---Select---" Value="" />   
</asp:DropDownList>

通常どおりコード ビハインドで DropDown をデータソースにバインドできるようになりました。このデータ ソースにはデフォルト値の項目を含める必要はありません。

IEnumerable<MyViewModel> model = ...
DropDownListSubContractors.DataSource = model;
DropDownListSubContractors.DataBind();
于 2013-02-04T12:34:20.227 に答える
2

これは次の方法で実行できます。

コードビハインドから:

DropDownListSubContractors.Items.Insert(0, new ListItem("---Select---", string.Empty));

注: インデックス 0 を使用して、リストの最初の要素にします。

于 2013-02-04T14:12:37.543 に答える