C# how to set dropDownList default value for selectedValue = null
Solution 1
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))
Solution 2
<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>
Solution 3
SelectedValue of a dropdownlist would never be null. It could be empty string, but it can never be null...
Solution 4
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
Comments
-
Com.Man.Do.Girl almost 4 years
I have a
dropdownlist1
that has has 6 collection items includingSelect One
. What I want is this ddl is set toSelectedvalue
= null. But what I am getting is my ddl always selectingSelect One
as its initial value. My ddl properties forSelect One
is selected:false. How to set this ddl to initial selected value = 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; }
-
Com.Man.Do.Girl almost 13 yearsI did as you said but when I insert something in txtProductName, it not displayed in browser.
-
Com.Man.Do.Girl almost 13 years@Alex Still not displayed.You see, my ddl somehow affecting textbox txtProductName. it Should display what I type in txtProductName first. Should i leave it be, and I select ddl item, then selectedValue will fill in. Or, I can leave both empty, still getting result as i want.
-
Com.Man.Do.Girl almost 13 yearsHowever weird it is, my problem regarding to this question is being solved by only replacing txtProductName.Text = ""; with SqlProductmaster.InsertParameters["ProductName"].DefaultValue = txtProductName.Text; Working like charm though.