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

标签 c# .net 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;
}

最佳答案

您可以添加“空”值(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/

相关文章:

c# - 将 xUnit.net Console Runner 与 Mono 结合使用

c# - 在VS 2008中添加dll

c# - 为 .Net 选择图表库

php - 在 Web 应用程序中查找瓶颈的方法有哪些?

javascript - Accordion 菜单时事件处理程序不触发

c# - NamedPipeServerStream 和 await 关键字

c# - 如何通过用户控件公开 ITemplate?

asp.net - IP 地址的经度和纬度值

.net - aspnetcore 2.2 api CORS 政策

c# - .NET 中的互操作性受损?