2011-06-23 93 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); 
相關問題