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

时间:2021-10-04 15:31:34

I have a dropdownlist1 that has has 6 collection items including Select One. What I want is this ddl is set to Selectedvalue = null. But what I am getting is my ddl always selecting Select One as its initial value. My ddl properties for Select One is selected:false. How to set this ddl to initial selected value = null?

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

7 个解决方案

#1


3  

You can add an 'Empty' value item:

您可以添加“空”值项:

<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>

Then you would check for

然后你会检查

if (string.IsNullOrEmpty(DropDownList1.SelectedValue))

#2


3  

<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server"> 
    <asp:ListItem Text="(Select a State)" Value="" />   
    <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>

#3


2  

SelectedValue of a dropdownlist would never be null. It could be empty string, but it can never be null...

下拉列表的SelectedValue永远不会为null。它可能是空字符串,但它永远不能为空...

#4


1  

It would be better to set the Items in this fashion

以这种方式设置项目会更好

<asp:ListItem Text="Select One" Value=""></asp:ListItem>

Also SelectedValue of the Dropdown is a string property so better check the same using string.IsNullOrEmpty as it cannot be null, leave the value blank where you want to consider it as null and repeat the same values in the Text and Value part for others

Dropdown的SelectedValue也是一个字符串属性,所以最好使用string.IsNullOrEmpty来检查它,因为它不能为null,将值留空到您想要将其视为null的位置,并在Text和Value部分中为其他值重复相同的值

#5


0  

Hera add a default value in the lsit...

赫拉在lsit中添加一个默认值...

<asp:DropDownList ID="DropDownList1" runat="server" Width="146px">
<asp:ListItem Selected="True">Select One</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>

but try something like seleted item instead of selectedvalue beacuse u have'nt defined values in the list items..

但尝试像seleted项目而不是selectedvalue因为你没有在列表项中定义的值..

    if (string.IsNullOrEmpty(DropDownList1.SeletedItem))  

For textbox also

对于文本框也

  txtProductName = DropDownList1.SeletedItem).ToString();

#6


0  

Make the select value="" that is empty and pass this to your stored procedure like this

使select value =“”为空,并将其传递给您的存储过程,如下所示

 dt = ta.GetData(
            String.IsNullOrEmpty(DropDownList1.SelectedValue)? null : DropDownList1.SelectedValue,
            String.IsNullOrEmpty(DropDownList2.SelectedValue) ? null : DropDownList2.SelectedValue,
            String.IsNullOrEmpty(DropDownList3.SelectedValue) ? null : DropDownList3.SelectedValue, null);

#7


-1  

Use CancelSelectOnNullParameter = False on the SqlDataSource

在SqlDataSource上使用CancelSelectOnNullParameter = False

#1


3  

You can add an 'Empty' value item:

您可以添加“空”值项:

<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>

Then you would check for

然后你会检查

if (string.IsNullOrEmpty(DropDownList1.SelectedValue))

#2


3  

<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server"> 
    <asp:ListItem Text="(Select a State)" Value="" />   
    <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>

#3


2  

SelectedValue of a dropdownlist would never be null. It could be empty string, but it can never be null...

下拉列表的SelectedValue永远不会为null。它可能是空字符串,但它永远不能为空...

#4


1  

It would be better to set the Items in this fashion

以这种方式设置项目会更好

<asp:ListItem Text="Select One" Value=""></asp:ListItem>

Also SelectedValue of the Dropdown is a string property so better check the same using string.IsNullOrEmpty as it cannot be null, leave the value blank where you want to consider it as null and repeat the same values in the Text and Value part for others

Dropdown的SelectedValue也是一个字符串属性,所以最好使用string.IsNullOrEmpty来检查它,因为它不能为null,将值留空到您想要将其视为null的位置,并在Text和Value部分中为其他值重复相同的值

#5


0  

Hera add a default value in the lsit...

赫拉在lsit中添加一个默认值...

<asp:DropDownList ID="DropDownList1" runat="server" Width="146px">
<asp:ListItem Selected="True">Select One</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>

but try something like seleted item instead of selectedvalue beacuse u have'nt defined values in the list items..

但尝试像seleted项目而不是selectedvalue因为你没有在列表项中定义的值..

    if (string.IsNullOrEmpty(DropDownList1.SeletedItem))  

For textbox also

对于文本框也

  txtProductName = DropDownList1.SeletedItem).ToString();

#6


0  

Make the select value="" that is empty and pass this to your stored procedure like this

使select value =“”为空,并将其传递给您的存储过程,如下所示

 dt = ta.GetData(
            String.IsNullOrEmpty(DropDownList1.SelectedValue)? null : DropDownList1.SelectedValue,
            String.IsNullOrEmpty(DropDownList2.SelectedValue) ? null : DropDownList2.SelectedValue,
            String.IsNullOrEmpty(DropDownList3.SelectedValue) ? null : DropDownList3.SelectedValue, null);

#7


-1  

Use CancelSelectOnNullParameter = False on the SqlDataSource

在SqlDataSource上使用CancelSelectOnNullParameter = False