我有一个 dropdownlist1
,它有 6 个集合项,包括 Select One
。我想要的是将此 ddl 设置为 Selectedvalue
= null。但我得到的是我的 ddl 总是选择 Select One
作为它的初始值。我的 Select One
的 ddl 属性被选中:false。如何将此 ddl 设置为初始选定值 = null?
<asp:DropDownList ID="DropDownList1" runat="server" Visible="False" Width="146px">
<asp:ListItem>Ceiling Speaker</asp:ListItem>
<asp:ListItem>Remote Microphone</asp:ListItem>
<asp:ListItem>Digital Source Player</asp:ListItem>
<asp:ListItem>Remote paging Console</asp:ListItem>
<asp:ListItem>Modular Mixer</asp:ListItem>
<asp:ListItem>Select One</asp:ListItem>
</asp:DropDownList>
if (String.IsNullOrEmpty(txtSearchProductname.Text))
{
if (DropDownList1.SelectedValue == null)
{
txtProductName.Text = "";
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = DropDownList1.SelectedValue.ToString();
}
}
else
{
SqlProductmaster.InsertParameters["ProductName"].DefaultValue = txtProductName.Text;
}
最佳答案
您可以添加“空”值(value)项目:
<asp:DropDownList ID="DropDownList1" runat="server" Width="146px">
<asp:ListItem Selected="True"></asp:ListItem>
<asp:ListItem>Ceiling Speaker</asp:ListItem>
<asp:ListItem>Remote Microphone</asp:ListItem>
<asp:ListItem>Digital Source Player</asp:ListItem>
<asp:ListItem>Remote paging Console</asp:ListItem>
<asp:ListItem>Modular Mixer</asp:ListItem>
<asp:ListItem>Select One</asp:ListItem>
</asp:DropDownList>
然后你会检查
if (string.IsNullOrEmpty(DropDownList1.SelectedValue))
关于C# 如何为 selectedValue = null 设置 dropDownList 默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6450505/