Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 594
Com.Man.Do.Girl Avatar asked Jun 23 '11 07:06

Com.Man.Do.Girl


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

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))
like image 81
Alex Aza Avatar answered Sep 18 '22 11:09

Alex Aza


<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>
like image 23
Bibhu Avatar answered Sep 17 '22 11:09

Bibhu