drop down list value in asp.net

2019-02-22 02:38发布

问题:

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

回答1:

<asp:DropDownList ID="DdlMonths" runat="server">
    <asp:ListItem Enabled="true" Text="Select Month" Value="-1"></asp:ListItem>
    <asp:ListItem Text="January" Value="1"></asp:ListItem>
    <asp:ListItem Text="February" Value="2"></asp:ListItem>
    ....
    <asp:ListItem Text="December" Value="12"></asp:ListItem>
</asp:DropDownList>

You can even use a RequiredFieldValidator which ignore this item, it considers it as unselected.

<asp:RequiredFieldValidator ID="ReqMonth" runat="server" ControlToValidate="DdlMonths"
    InitialValue="-1">
</asp:RequiredFieldValidator>


回答2:

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.



回答3:

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.



回答4:

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



回答5:

Say you have a drop down called ddlMonths:

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


回答6:

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>


回答7:

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


回答8:

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>


回答9:

You can try this

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