drop down list value in asp.net

前端 未结 9 1912
刺人心
刺人心 2021-01-18 08:29

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 sho

相关标签:
9条回答
  • 2021-01-18 08:47

    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

    0 讨论(0)
  • 2021-01-18 08:49

    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.

    0 讨论(0)
  • 2021-01-18 08:49

    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>
    
    0 讨论(0)
提交回复
热议问题