2011-06-23 92 views
7

我有一个dropdownlist1有6收集项目,包括Select One。我想要的是这个ddl设置为Selectedvalue = null。但是我得到的是我的ddl总是选择Select One作为它的初始值。我选择了Select One的ddl属性:false。如何将此ddl设置为初始选定值= null?C#如何设置dropDownList默认值为selectedValue = 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; 
} 

回答

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> 

,那么你会检查

if (string.IsNullOrEmpty(DropDownList1.SelectedValue)) 
+0

我照你所说的做了,但是当我在txtProductName中插入某些东西时,它并没有显示在浏览器中。 –

+0

然而奇怪的是,我对这个问题的问题只能通过替换txtProductName.Text =“”;来解决。与SqlProductmaster.InsertParameters [“ProductName”]。DefaultValue = txtProductName.Text; 虽然工作像魅力。 –

2

dropdownlist的SelectedValue永远不会为null。这可能是空字符串,但决不能为空...

+0

这是正确的答案。 –

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

@Alex仍然没有显示。你看,我的DDL以某种方式影响文本框txtProductName。它应该显示我在txtProductName中输入的内容。我应该离开它,我选择DDL项目,然后选择VALUE将填写。或者,我可以离开这两个空,仍然得到结果,因为我想。 –

1

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

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

同样下拉的SelectedValue是一个字符串属性,以便更好地查看使用string.IsNullOrEmpty一样,它不能为空,要考虑它为空,重复相同的值在TextValue一部分人

保留此值为空
0

赫拉在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> 

但尽量怎么一回事,因为ü列表中的项目have'nt定义的值类似入围项目代替的SelectedValue ..

if (string.IsNullOrEmpty(DropDownList1.SeletedItem)) 

对于文本框也

txtProductName = DropDownList1.SeletedItem).ToString(); 
+0

txtProductName填写不工作时。 –

-1

使用CancelSelectOnNullParameter =假上的SqlDataSource

0

请选择值= “” 那是空的,它传递给您的存储过程,这样

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); 
相关问题