[asp.net] drop down list value in asp.net

I want to add a value to the drop down list that cannot be selected, like a title. Ex: I have a month drop down list. The very first item should be "select month" this should not be selected. And next is from January to december. How can I do that?

And this is my current code.

string selectmonth = "select * from tblmonth";
SqlCommand scmselect = new SqlCommand(selectmonth, scnbuboy);

SqlDataReader sdrselect = scmselect.ExecuteReader();

drmonth.DataTextField = "month";
drmonth.DataValueField = "monthID";
drmonth.DataSource = sdrselect;

drmonth.DataBind();

This question is related to asp.net drop-down-menu

The answer is


You can add an empty value such as:

ddlmonths.Items.Insert(0, new ListItem("Select Month", ""))

And just add a validation to prevent chosing empty option such as asp:RequiredFieldValidator.


VB Code:

Dim ListItem1 As New ListItem()
ListItem1.Text = "put anything here"
ListItem1.Value = "0"
drpTag.DataBind()
drpTag.Items.Insert(0, ListItem1)

View:

<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator" ControlToValidate="drpTag" 
  ValueToCompare="0">
</asp:CompareValidator>

try this one

    <asp:DropDownList ID="ddList" runat="server">
    <asp:ListItem Value="">--Select Month--</asp:ListItem>
    <asp:ListItem Value="1">January</asp:ListItem>
    <asp:ListItem Value="2">Feburary</asp:ListItem>
    ...
    <asp:ListItem Value="12">December</asp:ListItem>
    </asp:DropDownList>

Value should be empty for the default selected listitem, then it works fine


In simple way, Its not possible. Because DropdownList contain ListItem and it will be selected by default

But, you can use ValidationControl for that:

<asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID" Display="Dynamic" 
ValidationGroup="g1" runat="server" ControlToValidate="ControlID"
Text="*" ErrorMessage="ErrorMessage"></asp:RequiredFieldValidator>

To add items to drop down list with value in asp.net using c# just add below codes for example:

try
{
    SqlConnection conn = new SqlConnection(conStr);
    SqlCommand comm = conn.CreateCommand();
    comm = conn.CreateCommand();
    comm.CommandText = "SELECT title,gid from Groups";
    comm.CommandType = CommandType.Text;
    conn.Open();
    SqlDataReader dr = comm.ExecuteReader();
    while (dr.Read())
    {
       dropDownList.Items.Add(new 
       ListItem(dr[0].ToString(),dr[1].ToString()));
    }
}
catch (Exception e)
{
    lblText.Text = e.Message;
}
finally
{
  conn.Close();
}

Say you have a drop down called ddlMonths:

ddlMonths.Items.Insert(0,new ListItem("Select a month","-1");

These are ALL great answers if you want to work that hard. But my guess is that you already have the items you want for the list coming from a databound element, and only want to add to the top of that list the "Hey, dude - pick one!" option. Assuming that is the case...

Here's the EASY Answer. And it ALWAYS works...

  1. Do your Databound List just like you planned.
  2. THEN, in Visual Studio, edit the items on dropdown,
  3. Add ONE MANUAL ITEM, make that your "Select an Item" choice,
  4. Using the properties window for the item in VS2012, check it as selected. Now close that window.
  5. Now, go to the properties box in Visual Studio on the lower left hand (make sure the dropdown is selected), and look for the property "AppendDataBoundItems".
  6. It will read False, set this to True.

Now you will get a Drop Down with all of your data items in it, PRECEDED BY your "Select an Item" statement made in the manual item. Try giving it a default value if possible, this will eliminate any errors you may encounter. The default is Zero, so if zero is not a problem, then leave it alone, if zero IS a problem, replace the default zero in the item with something that will NOT crash your code.

And stop working so hard...that's what Visual Studio is for.


You can try this

your_ddl_id.Items.Insert(0,new ListItem("Select","");