如何在 ASP.NET 中设置数据绑定(bind)下拉列表的初始值?
例如,我想要这些值,但要显示的第一个值应该是 -- Select One ---,且值为空。
最佳答案
我想你想做的是这样的:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="--Select One--" Value="" />
</asp:DropDownList>
确保“AppendDataBoundItems”设置为true,否则在绑定(bind)数据时将清除“--Select One--”列表项。
如果您将下拉列表的“AutoPostBack”属性设置为true,则还必须将“CausesValidation”属性设置为true,然后使用“requiredFieldValidator' 以确保“--Select One--”选项不会导致回发。
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1"></asp:RequiredFieldValidator>
关于asp.net - 数据绑定(bind)下拉列表 - 初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/521334/