Com.Man.Do.Girl
Com.Man.Do.Girl

Reputation: 177

C# how to set dropDownList default value for selectedValue = null

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?

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

Upvotes: 7

Views: 82387

Answers (7)

TheTechGuy
TheTechGuy

Reputation: 17394

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

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

Upvotes: 0

Lingo
Lingo

Reputation: 600

Use CancelSelectOnNullParameter = False on the SqlDataSource

Upvotes: 0

Syeda
Syeda

Reputation: 1205

Hera add a default value in the 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..

    if (string.IsNullOrEmpty(DropDownList1.SeletedItem))  

For textbox also

  txtProductName = DropDownList1.SeletedItem).ToString();

Upvotes: 0

Bibhu
Bibhu

Reputation: 4091

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

Upvotes: 3

V4Vendetta
V4Vendetta

Reputation: 38230

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

Upvotes: 1

Alex Aza
Alex Aza

Reputation: 78537

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

Upvotes: 3

Naveed Butt
Naveed Butt

Reputation: 2901

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

Upvotes: 2

Related Questions