DropDownList を使用する場合:
<asp:DropDownList ID="DropDownListSubContractors" runat="server"
DataTextField="Company_Name" DataValueField="id">
</asp:DropDownList>
リストの最初の値の代わりに、ドロップダウンの最初のオプションとして「---Select---」を使用できるようにする属性を使用/設定します。
DropDownList を使用する場合:
<asp:DropDownList ID="DropDownListSubContractors" runat="server"
DataTextField="Company_Name" DataValueField="id">
</asp:DropDownList>
リストの最初の値の代わりに、ドロップダウンの最初のオプションとして「---Select---」を使用できるようにする属性を使用/設定します。
使用できます
<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"));
<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();
これは次の方法で実行できます。
コードビハインドから:
DropDownListSubContractors.Items.Insert(0, new ListItem("---Select---", string.Empty));
注: インデックス 0 を使用して、リストの最初の要素にします。