C#如何为selectedValue = null设置dropDownList的默认值

Com*_*irl 8 .net c# asp.net drop-down-menu

我有一个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;
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*Aza 3

您可以添加“空”值项:

<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>
Run Code Online (Sandbox Code Playgroud)

然后你会检查

if (string.IsNullOrEmpty(DropDownList1.SelectedValue))
Run Code Online (Sandbox Code Playgroud)