gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:32:58 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com